Statement: Read 3 integer values and sort them in ascending order. At the end, show the values in ascending order, a blank line and then the values in the sequence as read.
public class ex10 {
public static void main(String[] args){
Scanner x = new Scanner(System.in);
System.out.println("Digite 3 valores em sequencia:");
int v1 = x.nextInt();
int v2 = x.nextInt();
int v3 = x.nextInt();
int um = 0;
int dois = 0;
int tres = 0;
if(v1 < v2 && v1 <v3){
um = v1;
}if(v2 < v3){
dois = v2;
tres = v3;
}else{
tres = v2;
dois = v3;
}
if(v2 < v1 && v2 < v3){
um = v2;
}if(v1 < v3){
dois = v1;
tres = v3;
}else{
dois = v3;
tres = v1;
}
if(v3 < v2 && v3 < v1){
um = v3;
}if(v2 < v1){
dois = v2;
tres = v1;
}else{
dois = v1;
tres = v2;
}
System.out.println("");
System.out.println(um);
System.out.println(dois);
System.out.println(tres);
System.out.println("");
System.out.println(v1);
System.out.println(v2);
System.out.println(v3);
}
}
My problem is this, when the user writes the numbers from highest to lowest (6,5,4) or from smallest to highest (4,5,6) the algorithm works correctly, but when the user puts out of order, eg 6,4,5 goes wrong. Another question is whether the algorithm could be done in a more concise way.