TernaryOperator Class

 

import java.util.Scanner;

public class TernaryOperator {

public static void main(String[] args) {
// Ternary Operator Example
int a, b, minVal;
a = 5;
b= 14;
Scanner input = new Scanner(System.in);
System.out.println("This program uses the ternary operator to determine the minimum value.");
System.out.println("Enter a value for A.");
a = input.nextInt();
System.out.println("Enter a value for B.");
b = input.nextInt();

minVal = (a < b) ? a : b;

System.out.println("A = " + a + " B = "+ b + " and the minValue is " + minVal);

System.out.println("This program uses an if/else statement to do the same thing");
System.out.println("Enter a value for A.");
a = input.nextInt();
System.out.println("Enter a value for B.");
b = input.nextInt();

if (a<b)
minVal = a;
else
minVal = b;

System.out.println("A = " + a + " B = "+ b + " and the minValue is " + minVal);


}

}