Java Online Compiler
Example: GreatestOfThreeUsingTernaryOperator in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// GreatestOfThreeUsingTernaryOperator import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Create a Scanner object Scanner scanner = new Scanner(System.in); // Step 2: Get three numbers from the user System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); System.out.print("Enter the third number: "); int num3 = scanner.nextInt(); // Step 3: Use the ternary operator to find the greatest number int greatest = (num1 >= num2 && num1 >= num3) ? num1 : (num2 >= num1 && num2 >= num3) ? num2 : num3; // Step 4: Print the result System.out.println(greatest + " is the greatest number."); // Step 5: Close the scanner scanner.close(); } }
Output
Clear
ADVERTISEMENTS