如果是刚接触或者刚学习Java,练习一些基础的算法还是必须的,可以提升思维和语法的使用。
1、输出两个int数中的最大值
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请依次输入两个整数:a,b(以空格隔开)");
/*比较两个数的大小*/
int a = scanner.nextInt();
int b = scanner.nextInt();
int max;
if(a >= b){
max = a;
}else {
max = b;
}
System.out.println("最大值为"+max);
}
}
}
2、输出三个int数中的最大值
package demo;
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请依次输入两个整数:a,b(以空格隔开)");
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
scanner.close();
/*方法一*/
int d=(a>b)?a:b;
int e=(d>c)?d:c;
System.out.println("最大值为"+e);
/*方法二*/
if(a>b && a>c){
System.out.println("最大值为"+a);
}else if(b>c && b>a){
System.out.println("最大值为"+b);
}else if(c>b && c>a){
System.out.println("最大值为"+c);
}else{
System.out.println("出现异常");
}
}
}
3、编写程序判断某一个年份是否是闰年
package demo;
import java.util.Scanner;
/*判断闰年
由用户输入任意一个年份,能被4整除但不能被100整除,或者能被400整除,是闰年。
要求判断一个年份是否为闰年。
要求输出:此年份是否是闰年
*/
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年份:");
int year = scanner.nextInt();
/*方法一*/
if((year % 4 ==0 && year % 100 !=0) || year%400 ==0){
System.out.println("这个年份是闰年");
}else{
System.out.println("这个年份不是闰年");
}
/*方法二*/
boolean isLeapYear = (year % 4 ==0 && year % 100 !=0) || year%400 ==0;
String string = isLeapYear?year+"是闰年":year+"不是闰年";
System.out.println(string);
}
}
**4、完成成绩等级输出程序 **如果用户输入的分数正确(0-100),则根据表-1中的规则计算该分数所对应的的级别,并计算结果。
package demo;
import java.util.Scanner;
/*
* 成绩等级划分表
* >= 90 A
* >=80 B
* >=60 C
* <60 D
*
* 分数范围:0-100
*
* 需要有2个判断*/
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入分数:");
double score = scanner.nextDouble();
scanner.close();
if(score < 0 || score >100){
System.out.println("输入的分数不在0-100之间,不符合要求");
}else if(score >= 90){
System.out.println("A");
}else if(score >= 80){
System.out.println("B");
}else if(score >= 60){
System.out.println("C");
}else{
System.out.println("D");
}
}
}
5、完成命令解析程序 问题:有一个命令解析程序,该程序提供三个功能选型供用户选择,用户选择某功能后,程序在界面上输出用户所选择的的功能名称。程序的交互如图:
package demo;
import java.util.Scanner;
/*
* 有一个命令解析程序,该程序提供三个功能选型供用户选择,
* 用户选择某功能后,程序在界面上输出用户所选择的的功能名称。
*
* */
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择功能:1.显示全部记录 2.查询登录记录 0.退出");
int command = scanner.nextInt();
scanner.close();
switch (command) {
case 0:
System.out.println("欢迎使用");
break;
case 1:
System.out.println("显示全部记录……");
break;
case 2:
System.out.println("查询登录记录……");
break;
default:
System.out.println("输入错误!");
}
}
}
6、完成收银柜台收款程序 编写一个收银柜台收款程序。根据单价、购买数量以及收款进行计算并输出应收金额和找零;当总价大于或者等于500,享受8折优惠。控制台交互如下:
package demo;
import java.util.Scanner;
/*
* 需求:
* 编写一个收银柜台收款程序。根据单价、购买数量以及收款进行计算并输出应收金额和找零;
* 当总价大于或者等于500,享受8折优惠。
*
*/
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入单价(¥):");
double price = scanner.nextDouble();
System.out.println("请输入数量:");
double amount = scanner.nextDouble();
System.out.println("请输入收款金额:");
double count = scanner.nextDouble();
double totalMoney = price*amount;
if(totalMoney > 500){
totalMoney = totalMoney*0.8;
}
double change = count - totalMoney;
System.out.println("应收金额为:"+totalMoney + "找零为:"+change);
}
}
7、java从键盘输入三个整数,实现从小到大排序
package demo;
import java.util.Scanner;
/*
* java从键盘输入三个整数,实现从小到大排序
*
**/
public class demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入三个整数,以空格隔开:");
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
scanner.close();
System.out.println("输入的值为:a = " + a + ", b = " + b + ", c = " + c);
if(a > b){
if ( b > c) {
System.out.println("排序后的值为:" + c + "," + b + "," + a);
}else if( c > a){
System.out.println("排序后的值为:" + b + "," + a + "," + c);
}else{
System.out.println("排序后的值为:" + b + "," + a + "," + c);
}
}else{
if(c < a){
System.out.println("排序后的值为:" + c + "," + a + "," + b);
}else if(c > b){
System.out.println("排序后的值为:" + a + "," + b + "," + c);
}else{
System.out.println("排序后的值为:"+ a + "," + c + "," + b);
}
}
}
}
8、计算个人所得税 北京地区的个人所得税计算公式: 应纳税额 = (工资薪金所得 - 扣除数)*适用税率 - 速算扣除数 其中,扣除数为3500,适用税率以及速算扣除数如下表所示:
package demo;
import java.util.Scanner;
/*
* 北京地区的个人所得税计算公式:
应纳税额 = (工资薪金所得 - 扣除数)*适用税率 - 速算扣除数
其中,扣除数为3500
*/
public class demo9 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的税前工资:");
int salaryBeforeTax = scanner.nextInt();
scanner.close();
int taxSalary = salaryBeforeTax - 3500;
double tax;
/* 方法一*/
tax = taxSalary<0?0.0:
taxSalary<=1500?taxSalary*0.03:
taxSalary<=4500?taxSalary*0.1-105:
taxSalary<=9000?taxSalary*0.2-555:
taxSalary<=35000?taxSalary*0.25-1005:
taxSalary<=55000?taxSalary*0.3-2755:
taxSalary<=80000?taxSalary*0.35-5505:
taxSalary*0.45-13505;
System.out.println("个人应缴纳税款为:"+tax);
/*方法二*/
if( taxSalary < 0 ){
tax = 0;
}else if( taxSalary <= 1500){
tax = taxSalary*0.03;
}else if( taxSalary <= 4500){
tax = taxSalary*0.1-105;
}else if( taxSalary <= 9000){
tax = taxSalary*0.2-555;
}else if( taxSalary <= 35000){
tax = taxSalary*0.25-1005;
}else if( taxSalary <= 55000){
tax = taxSalary*0.3-2755;
}else if( taxSalary <= 80000){
tax = taxSalary*0.35-5505;
}else{
tax = taxSalary*0.45-13505;
}
System.out.println("个人应缴纳税款为:"+tax);
}
}
9、输入年份和月份,输出天数
package demo;
import java.util.Scanner;
/*
提示:
1.需要判断是否是闰年,2月份的天数跟是否是闰年有关系;
2.用switch-case判断每个月的天数
*/
public class demo10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年份:");
int year = scanner.nextInt();
System.out.println("请输入月份:");
int month = scanner.nextInt();
int dayNum = theDayNum(month); //先根据月份得出天数,如果是闰年,对2月份的天数重新获取
if(isLeapYear(year)){
if(month == 2){
dayNum ++; //如果是闰年,2月份增加一天
}
System.out.print(year + "是闰年,");
}else{
System.out.print(year + "不是闰年,");
}
System.out.println(year + "年" + month + "月份共有" + dayNum + "天");
}
/*判断是否是闰年
* 能被4整除但不能被100整除,或者能被400整除,是闰年
*/
public static boolean isLeapYear(int year) {
if((year % 4 ==0 && year % 100 !=0) || year%400 ==0){
return true;
}else{
return false;
}
}
/*判断天数*/
public static int theDayNum(int month) {
switch (month) {
case 1:
return 31;
case 2:
return 28;
case 3:
return 31;
case 4:
return 30;
case 5:
return 31;
case 6:
return 30;
case 7:
return 31;
case 8:
return 31;
case 9:
return 30;
case 10:
return 31;
case 11:
return 30;
case 12:
return 31;
default:
System.out.println("对不起,您输入的月份有误!");
return 0;
}
}
}
作者:Wendy-lxq
原文地址:http://www.wxapp-union.com/portal.php?mod=view&aid=2893