实现对三个整数进行排序,输出时按照从小到大的顺序输出。
程序员文章站
2022-03-03 16:09:18
...
import java.util.Scanner;
class Test7
{
public static void main(String[] args){
Scanner sca = new Scanner(System.in);
System.out.println("第一个整数:");
int num1 = sca.nextInt();
System.out.println("第二个整数:");
int num2 = sca.nextInt();
System.out.println("第三个整数:");
int num3 = sca.nextInt(); //输入三个整数
if(num1 >= num2){ //num1 >= num2的所有情况
if(num2 >= num3){
System.out.println(num3 + "," + num2 + "," + num1);
}else if(num3 >= num1){
System.out.println(num2 + "," + num1 + "," + num3);
}else{
System.out.println(num2 + "," + num3 + "," + num1);
}
}else{ //num1 >= num2的所有情况
if(num2 <= num3){
System.out.println(num1 + "," + num2 + "," + num3);
}else if(num3 <= num1){
System.out.println(num3 + "," + num1 + "," + num2);
}else{
System.out.println(num1 + "," + num3 + "," + num2);
}
}
}
}
上一篇: 第十讲 对象
下一篇: 1.1 将序列分解为单独的变量