简体   繁体   English

以升序输出数字(if - else)java

[英]Output numbers in ascending order (if - else) java

Tell me what is missing?告诉我缺少什么? Two values are sorted correctly, but the average is not :(两个值排序正确,但平均值不是:(

package com.Star;

public class Main {

    public static void main(String[] args) {

        int a = 89;
        int b = 96;
        int c = 88;

        if (a < b & a < c)
            System.out.println(a + " " + b + " " + c);

        if (b < c & b < a)
            System.out.println(b + " " + c + " " + a);

        if (c < a & c < b)
            System.out.println(c + " " + b + " " + a);

    }
}

This is not a right way to do this, but if you replace & with && you can get a partially correct code, since you have to check 6 permutation for 3 elements (nPr).这不是执行此操作的正确方法,但是如果您将 & 替换为 && 您可以获得部分正确的代码,因为您必须检查 3 个元素的 6 个排列 (nPr)。 Use Array and apply sorting method.使用 Array 并应用排序方法。

 public static void main(String[] args) {

        int a = 89;
        int b = 96;
        int c = 88;

        if (a < b && b < c)
            System.out.println(a + " " + b + " " + c);
        else if (a < c && c < b)
            System.out.println(a + " " + c + " " + b);
        else if (b < a && a < c)
            System.out.println(b + " " + a + " " + c);
        else if (b < c && c < a)
            System.out.println(b + " " + c + " " + a);
        else if (c < a && a < b)
            System.out.println(c + " " + a + " " + b);
        else if (c < b && b < a)
            System.out.println(c + " " + b + " " + a);

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM