简体   繁体   中英

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). Use Array and apply sorting method.

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

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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