1.练习一
答案
package day02;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.println("请输入一个5位数(例如:12345)");
int num=input.nextInt();
System.out.println(num+"的个位数是:"+num%10);
System.out.println(num+"的十位数是:"+num/10%10);
System.out.println(num+"的百位数是:"+num/100%10);
System.out.println(num+"的千位数是:"+num/1000%10);
System.out.println(num+"的万位数是:"+num/10000%10);
}
}
2.练习二
答案
package day02;
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入你的存款:");
int num=input.nextInt();
if(num>5000000){
System.out.println("我买凯迪拉克");
}
else if(num>1000000&&num<=5000000){
System.out.println("我买帕萨特");
}
else if(num>500000&&num<=1000000){
System.out.println("我买依兰特");
}
else if(num>100000&&num<=500000){
System.out.println("我买奥托");
}
else if(num<=100000){
System.out.println("我买捷安特");
}
}
}
3.练习三
答案
package day02;
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入年份:");
int year = input.nextInt();
System.out.println("请输入月份:");
int month = input.nextInt();
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(month + "月为31天");
break;
}
switch (month) {
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println(month + "月为29天");
} else {
System.out.println(month + "月为28天");
}
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(month + "月为30天");
break;
}
}
}
4.练习四
答案
package day02;
import java.util.Scanner;
public class Demo6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入消费金额:");
int money = input.nextInt();
System.out.println("是否参加优惠换购活动:");
System.out.println(
"1:满50元,加2元换购百事可乐饮料一瓶\n" +
"2:满100元,加3元换购500ml可乐一瓶\n" +
"3:满100元,加10元换购5公斤面粉\n" +
"4:满200元,加10元换购1个苏泊尔炒菜锅\n" +
"5:满200元,加20元可换购欧莱雅爽肤水一瓶\n" +
"0:不换购"
);
System.out.println("请选择:");
int num = input.nextInt();
switch (num) {
case 1:
if (money >= 50) {
money += 2;
System.out.println("本次消费总金额:" + money);
System.out.println("成功换购:百事可乐饮料一瓶。");
} else {
System.out.println("您的消费金额不足,不能换购");
}
break;
case 2:
if (money >= 100) {
money += 3;
System.out.println("本次消费总金额:" + money);
System.out.println("成功换购:500ml可乐一瓶。");
} else {
System.out.println("您的消费金额不足,不能换购");
}
break;
case 3:
if (money >= 100) {
money += 10;
System.out.println("本次消费总金额:" + money);
System.out.println("成功换购:5公斤面粉。");
} else {
System.out.println("您的消费金额不足,不能换购");
}
break;
case 4:
if (money >= 200) {
money += 10;
System.out.println("本次消费总金额:" + money);
System.out.println("成功换购:苏泊尔炒菜锅。");
} else {
System.out.println("您的消费金额不足,不能换购");
}
break;
case 5:
if (money >= 200) {
money += 20;
System.out.println("本次消费总金额:" + money);
System.out.println("成功换购:欧莱雅爽肤水。");
} else {
System.out.println("您的消费金额不足,不能换购");
}
break;
}
}
}
5.小结
以上是一些简单的Java练习题,希望可以给Java初学者提供一点帮助,上述答案方法不唯一,只要能达到题目要求就都是正确的。