'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);
}
}
Solution 1:[1]
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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
