重要:能想明白输入数字之间大小比较的逻辑方式
与之前相同,有关输入类型相关的,就需要引用 Scanner方法
代码:不要忘记先 import java.util.Scanner;
1、复杂版-同时也是最开始思考时的思路版本
int save_number1 = 0, save_number2 = 0, save_number3 = 0;for (int i = 1; i < 4; i++) {
System.out.println("请输入第" + i + "个数字");
Scanner get_number = new Scanner(System.in);
switch (i) {
case 1:
save_number1 = get_number.nextInt();
break;
case 2:
save_number2 = get_number.nextInt();
break;
case 3:
save_number3 = get_number.nextInt();
break;
}
}
if (save_number1 > save_number2) {
if (save_number1 > save_number3) {
if (save_number2 > save_number3) {
System.out.println(save_number1 + ">" + save_number2 + ">" + save_number3);
} else {
System.out.println(save_number1 + ">" + save_number3 + ">" + save_number2);
}
} else {
System.out.println(save_number3 + ">" + save_number1 + ">" + save_number2);
}
} else if (save_number1 < save_number2) {
if (save_number2 > save_number3) {
if (save_number1 < save_number3) {
System.out.println(save_number2 + ">" + save_number3 + ">" + save_number1);
} else {
System.out.println(save_number2 + ">" + save_number1 + ">" + save_number3);
}
} else {
System.out.println(save_number3 + ">" + save_number2 + ">" + save_number1);
}
} 2、简化版 System.out.println("请输入第一个数字:");
Scanner s1 = new Scanner(System.in);
int S1 = s1.nextInt();
System.out.println("请输入第二个数字:");
Scanner s2 = new Scanner(System.in);
int S2 = s2.nextInt();
System.out.println("请输入第三个数字:");
Scanner s3 = new Scanner(System.in);
int S3 = s3.nextInt();
if (S1 > S2) {
S1 += S2;
S2 = S1 - S2;
S1 = S1 - S2;
}
if (S2 > S3) {
S2 += S3;
S3 = S2 - S3;
S2 = S2 - S3;
}
if (S1 > S2) {
S1 += S2;
S2 = S1 - S2;
S1 = S1 - S2;
}
System.out.println("三个数字由小到大排序为:" + S1 + "<" + S2 + "<" + S3); 标签:Java,入门,S2,S1,System,number3,number1,save,输入 From: https://www.cnblogs.com/Lee597/p/17603615.html