Advertisement
476179

actsSwirtch

Nov 4th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. package act;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class SwitchStatAct
  6. { //1
  7. //Write a program that uses a switch statement to display "one" if the user
  8. //has entered 1, "two" if the user has entered 2, and "three" if the user has
  9. //entered 3. If a number other than 1, 2, or 3 is entered, the program should display an error message. Ensure your program begins by prompting the user to enter 1, 2, or 3.
  10.  
  11. public static void main(String[] args)
  12. {
  13. Scanner keyboard = new Scanner(System.in);
  14.  
  15. int num;
  16. System.out.println("enter 1, 2, or 3:");
  17. num = keyboard.nextInt();
  18.  
  19. switch (num)
  20. {
  21. case 1:
  22. System.out.println("one");
  23. break;
  24.  
  25.  
  26. case 2:
  27. System.out.println("two");
  28. break;
  29.  
  30. case 3:
  31. System.out.println("three");
  32. break;
  33.  
  34. default:
  35. System.out.println("you must enter 1, 2 or 3.");
  36. break;
  37. }
  38.  
  39. keyboard.close();
  40.  
  41.  
  42. //2
  43. /*
  44. Scanner keyboard = new Scanner(System.in);
  45. String inp;
  46. char in;
  47. System.out.println("enter A, B, or C:");
  48. inp = keyboard.nextInt();
  49. in = inp.charAt(0);
  50. switch (in)
  51. {
  52. case 'A':
  53. System.out.println("You selected A.");
  54. break;
  55.  
  56.  
  57. case 2:
  58. System.out.println("You selected B.");
  59. break;
  60.  
  61. case 3:
  62. System.out.println("You selected C.");
  63. break;
  64.  
  65. case 3:
  66. System.out.println("You selected C.");
  67. break;
  68.  
  69. default:
  70. System.out.println("you must enter 1, 2 or 3.");
  71. break;
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. */
  87.  
  88.  
  89. }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement