Advertisement
476179

Switch Statement activities2

Nov 8th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 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.  
  43. /*
  44. * #2
  45. Scanner keyboard = new Scanner(System.in);
  46. String inp;
  47. char in;
  48. System.out.println("enter A, B, or C:");
  49. inp = keyboard.nextInt();
  50. in = inp.charAt(0);
  51. switch (in)
  52. {
  53. case 'A':
  54. System.out.println("You selected A.");
  55. break;
  56.  
  57.  
  58. case 'B':
  59. System.out.println("You selected B.");
  60. break;
  61.  
  62. case 'C':
  63. System.out.println("You selected C.");
  64. break;
  65.  
  66. case 'D':
  67. System.out.println("You selected D.");
  68. break;
  69.  
  70. default:
  71. System.out.println("Not good with letters, eh?");
  72. break;
  73. }
  74.  
  75. ----------------------------------------------------
  76. #3
  77. you cannot convert this to a switch statement because cases must be exact answers not
  78. including > or < symbols
  79.  
  80. #4
  81. same reason as above, cases cannot include < or > symbols must be exact numbers
  82.  
  83. #5
  84. That is serious.
  85.  
  86.  
  87. */
  88.  
  89.  
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement