美文网首页
键盘输入三个数,从小到大输出

键盘输入三个数,从小到大输出

作者: 啷里个啷里个啷个里个啷 | 来源:发表于2021-06-08 20:36 被阅读0次
    /*
    编写程序:
        由键盘输入三个整数,分别存入变量num1,num2,num3,
        对它们进行排序(使用if-else ),并且从小到大输出。
    */
    
    import java.util.Scanner;
    
    class IfTest1 {
    
        public static void main(String[] args){
            
                   System.out.println("请输入第一个整数:");
            Scanner scan = new Scanner(System.in);
            int num1 = scan.nextInt();
    
            System.out.println("请输入第二个整数:");
            int num2 = scan.nextInt();
    
            System.out.println("请输入第三个整数:");
            int num3 = scan.nextInt();
    
            if (num1 >= num2) { // 2 1
    
                if (num3 >= num1) { // 2 1 3
                    System.out.println(num2 + "," + num1 + "," + num3);
                } else if(num3 <= num2 ){// 3 2 1
                    System.out.println(num3 + "," + num2 + "," + num1);
                } else {
                    System.out.println(num2 + "," + num3 + "," + num1);
                }
                
            } else { // 1 2 
    
                if (num3 >= num2) {
                    System.out.println(num1 + "," + num2 + "," + num3);
                } else if (num3 <= num1) {
                    System.out.println(num3 + "," + num1 + "," + num2);
                } else {
                    System.out.println(num1 + "," + num3 + "," + num2);
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:键盘输入三个数,从小到大输出

          本文链接:https://www.haomeiwen.com/subject/owissltx.html