用户输入3个整数,将这个3个整数从大到小打印出来
第一种做法
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if (a > b) {
if (b > c) {
System.out.println(a + "," + b + "," + c);
} else if (a < c) {
System.out.println(c + "," + a + "," + b);
} else {
System.out.println(a + "," + c + "," + b);
}
} else {
if (b < c) {
System.out.println(c + "," + b + "," + a);
} else if (a > c) {
System.out.println(b + "," + a + "," + c);
} else {
System.out.println(b + "," + c + "," + a);
}
}
}
}
第二种做法
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入三个数");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int t = 0;
if(a < b) {
t = b;
b = a;
a = t;
}
if(a < c) {
t = c;
c = a;
a = t;
}
if(b < c) {
t = c;
c = b;
b = t;
}
System.out.println(a+","+b+","+c);
}
}
心得
应该要充分理解Scanner的应用以及if…else的应用。继续加油!