Consider the following case statements from a switch/case block. Assume a menu o
ID: 3842965 • Letter: C
Question
Consider the following case statements from a switch/case block. Assume a menu of options where the user selects the number for the year desired, not the year, (e.g., the user wants the year 1985 so they select 3 from the menu of choices) and the menu was displayed to the user as follows:
1. 1983
2. 1984
3. 1985
4. 1986
5. 1987
What would the output be if case 1 were selected by the user? What about case 2? If there is/are issues with the case statements listed, how could they be fixed?
Scanner sc = new Scanner(System.in);
System.out.print("Select a number for the year: "); int year = sc.nextInt();
switch (year) {
case 1: System.out.println("Year of the Pig."); break;
case 2: System.out.println("Year of the Rat.");
case 3: System.out.println("Year of the Ox.");
case 4: System.out.println("Year of the Tiger.");
case 5: System.out.println("Year of the Hare."); }
Explanation / Answer
Answer: I have fixed the issue and highlighted the code changes below
Scanner sc = new Scanner(System.in);
System.out.print("Select a number for the year: "); int year = sc.nextInt();
switch (year) {
case 1: System.out.println("Year of the Pig."); break;
case 2: System.out.println("Year of the Rat.");break;
case 3: System.out.println("Year of the Ox.");break;
case 4: System.out.println("Year of the Tiger.");break;
case 5: System.out.println("Year of the Hare."); break;
}
If we are not fixing this issue then
What would the output be if case 1 were selected by the user?
Answer: Year of the Pig.
What about case 2?
Answer:
Year of the Rat.
Year of the Ox.
Year of the Tiger.
Year of the Hare.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.