Advertisement
Guest User

SwitchStatements

a guest
Jul 25th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. package switchstatments;
  2.  
  3. import java.util.Scanner;
  4. /**
  5. * @author Kitty
  6. */
  7. public class SwitchStatments {
  8.  
  9. /**
  10. * @param args the command line arguments
  11. */
  12. public static void main(String[] args) {
  13.  
  14. // introduces the keyboard object for the Scanner class
  15. Scanner keyboard = new Scanner(System.in);
  16.  
  17. // introduces the boolean that closes the menu and the integer that holds the value of the menu number
  18. boolean redo = true;
  19. int menuNum = 0;
  20.  
  21. // start of the do while loop
  22. do {
  23.  
  24. // asks user for input
  25. System.out.println("What menu do you want to go to");
  26.  
  27. // sets the value of the menu number to the value input by the user
  28. menuNum = keyboard.nextInt();
  29.  
  30. // start of the switch statement with the integer menuNum
  31. switch (menuNum){
  32.  
  33. // checks if the menuNum is 0
  34. case 0:
  35.  
  36. // makes the loop variable false and exits the program
  37. redo = false;
  38.  
  39. break;
  40.  
  41. // checks if the menuNum is 1
  42. case 1:
  43.  
  44. // sets the balue of redo to true so it will loop
  45. redo = true;
  46.  
  47. break;
  48.  
  49. // checks if the menuNum is 2
  50. case 2:
  51.  
  52. // sets the balue of redo to true so it will loop
  53. redo = true;
  54.  
  55. break;
  56. }
  57.  
  58. // tells the user the menu they are on
  59. System.out.println("You are in menu "+ menuNum);
  60.  
  61. // if the value of redo is true then it will loop
  62. } while (redo);
  63.  
  64. // exiting message
  65. System.out.println("You are exiting the program. Goodbye.");
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement