题目:
在主方法中通过键盘录入三个整数。定义一个方法,方法接收三个整数变量,在方法中从大到小依次打印三个变量。执行效果如下:
请输入第一个整数:10 请输入第二个整数:30 请输入第三个整数:20 从大到小的顺序是: 30 20 10
我写的代码:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Test test = new Test();
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
test.method(a,b,c);
}
public void method(int a,int b,int c){
int max = 0;
int mid = 0;
int min = 0;
if (a > b){
if (a > c){
max = a;
}else {
max = c;
}
}else {
if (b > c){
max = b;
}else {
max = c;
}
}
if (a < b){
if (a < c){
min = a;
}else {
min = c;
}
}else {
if (b < c){
min = b;
}else {
min = c;
}
}
mid = (a + b + c) - max - min;
System.out.println("从大到小的顺序是:" + max + " " + mid + " " + min);
}
代码有可能不是最优解答,如果有大佬也可以把更完美的代码发到评论区,或者给出优化的代码