0
Statement: Read 3 integers and order them in ascending order. At the end, show the values in ascending order, a blank line and then the values in 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 the largest to the smallest (6,5,4) or from the smallest to the largest (4,5,6) the algorithm works correctly, but when the user puts out of order, for example 6,4,5 goes wrong. Another question is whether it would be possible to do this algorithm in a more summarized way.
Joao, post the solution as an answer, adding to the question you leave her confused.
– user28595
Okay, I’ll do it!
– João Laurent
Possible duplicate of Greater and lesser number
– Francisco