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

 首先输入数字用到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);*/
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值