输入三个数,并按从小到大排序

 首先输入数字用到Scaner语句

第一种方式,赋值方式

这里我用的浮点型double

整数就用int

   Scanner sc = new Scanner(System.in);
        System.out.println("请输入三个实数:");
        double a = sc.nextDouble();
        double b = sc.nextDouble();
        double c = sc.nextDouble();
        double d;
        if(a>b){//459
            d = a;//4
            a = b;//5
            b = d;//4
        }
        if(a>c){
            d = a;
            a = c;
            c = d;
        }
        if(b>c){
            d = b;
            b = c;
            c = d;
        }
        System.out.println(a+"\t"+b+"\t"+c);

第二种:麻烦的三元 运算的方式,先求出最大值那个,剩下的在进行比较哪个小,或者先求出最小的那个,剩下的比较大

        Scanner sc = new Scanner(System.in);     
        System.out.println("输入第一个数");
        int a = sc.nextInt();
        System.out.println("输入第二个数");
        int b = sc.nextInt();
        System.out.println("输入第三个数");
        int c = sc.nextInt();//123,321,926
        //int min=a<b?a:b;//2
        int max1=a>b?a:b;//9
        int max2=a>c?a:c;//6
        int max=max1>max2?max1:max2;
        if (a==max){
          if (b>c) {
              System.out.println(c + "," + b + "," + a);
          }else System.out.println(b + "," + c + "," + a);
        }if(b==max){
            if (a>c){
                System.out.println(c + "," + a+ "," + b);
            }else System.out.println(a+ "," + c + "," + b);
        }if (c==max){
            if (a>b){
                System.out.println(b + "," + a+ "," + c);
            }else System.out.println(a + "," + b + "," + c);
        }

第三种:最死脑的方式一个个比较

if (c < a && c < b && b < a) {// 通过比较c<a c<b b<a
            System.out.println("最大" + a+"中间" + b+"最小" + c);
        } else if (c < a && b < c && b < a) {
            System.out.println("最大" + a+"中间" + c+"最小" +b);
        } else if (c < a && c < b && a < b) {
            System.out.println("最大" + b+"中间" + a+"最小" + c);
        } else if (a < c && a < b && c < b) {
            System.out.println("最大" + b+"中间" + c+"最小" + a);
        } else if (a < b && a < c && b < c) {
            System.out.println("最大" + c+"中间" + b+"最小" + a);
        } else if (b < a && b < c && a < c) {
            System.out.println("最大" + c+"最小" + a+"最小" + b);*/
        }

    原文作者:大数据开发者Cd
    原文地址: https://blog.csdn.net/qq_56795768/article/details/119190266
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞