Guest User

Untitled

a guest
Jun 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. public class MainMenu {
  2. public static void main(String[] args)
  3. {
  4. System.out.println("My First Java program can do many things!");
  5. System.out.println("1.Estimate populationn2.Generate random integern3. Print ASCII tablen4. Approximate pi by iteration");
  6. System.out.println("What would you like to do? (1-4)");
  7. System.out.print("Your choice: ");
  8. int input = 0;
  9. Scanner keyboard = new Scanner(System.in);
  10. switch (keyboard.nextInt())
  11. {
  12. case 1:
  13. System.out.println("You chose to estimate population.");
  14. break;
  15. case 2:
  16. System.out.println("You chose to generate random integer.");
  17. break;
  18. case 3:
  19. System.out.println("You chose to print ASCII table.");
  20. break;
  21. case 4:
  22. System.out.println("You chose to approximate pi by iteration.");
  23. break;
  24. }
  25.  
  26. if(input>4 || input<1)
  27. {
  28. System.out.println("Sorry, I don't know what to do. Please try again.");
  29. keyboard.next();
  30. if(!keyboard.hasNextInt())
  31. {
  32. System.out.println("Sorry, only integers allowed for this menu. Good-bye!");
  33. System.exit(0);
  34. }
  35. keyboard.next();
  36. input = keyboard.nextInt();
  37. keyboard.nextLine();
  38. }
  39.  
  40. import java.util.InputMismatchException;
  41. import java.util.Scanner;
  42. public class Test {
  43. public static void main(String[] args) {
  44. System.out.println("My First Java program can do many things!");
  45. System.out.println("1.Estimate populationn2.Generate random integern3. Print ASCII tablen4. Approximate pi by iteration");
  46. System.out.println("What would you like to do? (1-4)");
  47. System.out.print("Your choice: ");
  48.  
  49. Scanner keyboard = new Scanner(System.in);
  50. while (true) {
  51. int input = 0;
  52. try {
  53. input = keyboard.nextInt();
  54. } catch (InputMismatchException e) {
  55. System.out
  56. .println("Sorry, only integers allowed for this menu. Good-bye!");
  57. System.exit(0);
  58. }
  59. switch (input) {
  60. case 1:
  61. System.out.println("You chose to estimate population.");
  62. break;
  63. case 2:
  64. System.out.println("You chose to generate random integer.");
  65. break;
  66. case 3:
  67. System.out.println("You chose to print ASCII table.");
  68. break;
  69. case 4:
  70. System.out.println("You chose to approximate pi by iteration.");
  71. break;
  72. }
  73.  
  74. if (input > 4 || input < 1) {
  75. System.out
  76. .println("Sorry, I don't know what to do. Please try again.");
  77. }
  78. }
  79. }
  80. }
Add Comment
Please, Sign In to add comment