Page 58 - CTS - CSA TP - Volume 2
P. 58

COMPUTER SOFTWARE APPLICATION - CITS



           break;
           case 2:
           System.out.println(“World”);
           break;
           case 3:

           System.out.println(“Exiting the program.”);
           break;
           default:
           System.out.println(“Invalid choice”);
                   }
           scanner.close();
               }
           }
           Explanation :
           This Java program, named SimpleMenu, presents a basic menu system that allows users to select from a list of
           options and performs actions based on their selection. Here’s how it works:
           1  The program starts by importing the Scanner class from the java.util package to allow user input from the
              console.
           2  The SimpleMenu class contains the main method, which serves as the entry point of the program.
           3  Inside the main method:
              •  It creates a new Scanner object named scanner to read input from the console.
              •  It displays a menu to the user, prompting them to select an option:

              •  Option 1: Print “Hello”
              •  Option 2: Print “World”
              •  Option 3: Exit the program
              •  It reads the integer input provided by the user using the nextInt() method of the Scanner class and stores
                 it in the variable choice.
           4  The program then uses a switch statement to perform different actions based on the value of choice:
              •  If the user selects 1, it prints “Hello” to the console.
              •  If the user selects 2, it prints “World” to the console.
              •  If the user selects 3, it prints “Exiting the program.” and terminates.

              •  If the user selects any other value, it prints “Invalid choice” to the console.
           5  After executing the appropriate action, the program closes the scanner object to release system resources.
           Output:





















                                                           43

                               CITS : IT & ITES - Computer Software Application - Exercise 86
   53   54   55   56   57   58   59   60   61   62   63