键盘录入3个整数,并从小到大排序(if --else嵌套语句)

练习:从键盘录入 3 个整数,并且从小到大排序

import java.util.Scanner;
class IfTest {
	public static void main(String[] args) {
		// 创建键盘录入对象
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入第一个整数");
		int num1 = sc.nextInt();
		System.out.println("请输入第二个整数");
		int num2 = sc.nextInt();
		System.out.println("请输入第三个整数");
		int num3 = sc.nextInt();
		// if--else嵌套
		if (num1 >= num2){
			if (num3 >= num1){
				System.out.println(num2 + " " + num1 + " " + num3);
			}else if (num3 <= num2){
				System.out.println(num3 + " " + num2 + " " + num1);
			}else {
				System.out.println(num2 + " " + num3 + " " + num1);
			}
		}else{
			// num2 > num1
			if (num3 >= num2){
				System.out.println(num1 + " " + num2 + " " + num3);
			}else if (num3 <= num1){
				System.out.println(num1 + " " + num3 + " " + num2);
			}else {
				System.out.println(num1 + " " + num3 + " " + num2);
			}
		}
	}
}

上一篇:java数据类型和运算符


下一篇:Android 系统属性SystemProperty分析