Java Online Compiler
Example: GreatestOfThreeUsingNestedIfElse in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// GreatestOfThreeUsingNestedIfElse 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 nested if-else to find the greatest number if (num1 >= num2) { if (num1 >= num3) { System.out.println(num1 + " is the greatest number."); } else { System.out.println(num3 + " is the greatest number."); } } else { // num2 > num1 if (num2 >= num3) { System.out.println(num2 + " is the greatest number."); } else { System.out.println(num3 + " is the greatest number."); } } // Step 4: Close the scanner scanner.close(); } }
Output
Clear
ADVERTISEMENTS