user605989 user605989 - 29 days ago 13
Java Question

Display three non-negative integers in increasing order

Prompt the user to type in three non-negative integers. Display the integers in an increasing order.

import java.util.Scanner;

{
int number1,number2,number3;
Scanner keyboard = new Scanner (System.in);
number1=keyboard.nextInt();
number2=keyboard.nextInt();
number3=keyboard.nextInt();

if(number1<number2&&number1<number3&&number2<number3)
{
System.out.println(number1);
System.out.println(number2);
System.out.println(number3);
}

else if(number1>number2&&number1>number3&&number2>number3)
{
System.out.println(number3);
System.out.println(number2);
System.out.println(number1);
}

else if(number1<number2&&number1<number3&&number2>number3)
{
System.out.println(number1);
System.out.println(number3);
System.out.println(number2);
}

else if(number2>number1&&number2>number3&&number1>number3)
{
System.out.println(number3);
System.out.println(number1);
System.out.println(number2);
}

else if (number3>number1&&number3>2&&number1>number2)
{
System.out.println(number2);
System.out.println(number1);
System.out.println(number3);
}


Output:

2
1
3

1
2
3

P.S. I am not allowed to use while statement, swing and for. I am newbie at java so this what I could come up with. Is there a faster way? Thank you.

Answer
boolean n2 ;   
if(num1>num2) {
    if(num1>num2 && num1>num3){
        if(num2>num3){
            System.out.println(num1+">"+num2+">"+num3);
            n2=true;
        }
        else {
            System.out.println(num1+">"+num3+">"+num2);
        }
    }
    else if(n2){
        if(num1<num3){
            System.out.println(num2+">"+num3+">"+num1);
        }
    }
}
else {
    System.out.println(num2+">"+num1+">"+num3);
}