Conditional operator: ?

This example reads in two numbers and prints them out in order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Print two numbers in order
package numbers;
import java.util.*;
public class OrderNumbers {
  public static void main(String[] args) {
    Scanner keyboard=new Scanner(System.in); //do just once
    int num1,num2;
    System.out.print("Enter a number:");
    num1=keyboard.nextInt();
    System.out.print("Enter another number:");
    num2=keyboard.nextInt();
    System.out.println((num1<num2)? num1+" "+num2: num2+" "+num1);
  }//main
}//class
CODE

Sample output:

Enter a number:8
Enter another number:3
3 8

Enter a number:5
Enter another number:12
5 12

Enter a number:8
Enter another number:8
8 8

NEXT: