Advertisement
earlution

Java option menu - using switch

Sep 17th, 2021
1,254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class OptionMenu {
  3.    
  4.     public static void main(String [] args){
  5.        
  6.         Scanner scanner = new Scanner(System.in);
  7.         // Display the menu
  8.         System.out.println("1\t Menu option 1");
  9.         System.out.println("2\t Menu option 2");
  10.         System.out.println("3\t Menu option 3");
  11.         System.out.println("4\t Menu option 4");
  12.         System.out.println();
  13.         System.out.println("Please enter your choice:");
  14.        
  15.         //Get user's choice
  16.         int choice = scanner.nextInt();
  17.          
  18.         //Display the title of the chosen module
  19.         switch (choice) {
  20.             case 1: System.out.println("You choose the 1st menu option.");
  21.                 break;
  22.             case 2: System.out.println("You choose the second menu option.");
  23.                 break;
  24.             case 3: System.out.println("The third menu option, was the one you chose.");
  25.                 break;
  26.             case 4: System.out.println("The forth one from the top was chosen.");
  27.                 break;
  28.             default: System.out.println("Invalid choice");
  29.         }//end of switch
  30.     }//end of the main method
  31. }//end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement