输出数字按升序排列(if - else)java

时间:2016-04-11 21:24:00

标签: java if-statement

告诉我缺少什么?两个值正确排序,但平均值不是:(

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);

    }
}

1 个答案:

答案 0 :(得分:0)

这不是一种正确的方法,但如果你更换&amp;与&amp;&amp;您可以获得部分正确的代码,因为您必须检查3个元素(nPr)的6个排列。使用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);

        }