Question: Consider the following Java program that demonstrates the switch statement: import java.util.Scanner; // Needed for Scanner class public class SwitchDemo { public static void main(String[]

Consider the following Java program that demonstrates the switch statement:

import java.util.Scanner; // Needed for Scanner class

public class SwitchDemo {

public static void main(String[] args)

{

int number; // A number entered by the user

// Create a Scanner object for keyboard input.

Scanner keyboard = new Scanner(System.in);

// Get one of the numbers 1, 2, or 3 from the user.

System.out.print("Enter 1, 2, or 3: ");

number = keyboard.nextChar();

// Determine the number entered.

switch (number)

{

case 1:

System.out.println("You entered 1.");

break;

case 2:

System.out.println("You entered 2.");

break;

case 3:

System.out.println("You entered 3.");

break;

default:

System.out.println("That's not 1, 2, or 3!");

}

}

}

You are required to do the following:

a) Correct the syntax error(s) in this code so that it compiles and runs.

b) Once it runs, it would always display the output as That's not 1, 2, or 3! even if you enter 1, 2 or 3. That means it has some semantic or logical error, which you must correct, so that the program works correctly. It should give the output as You entered 1. when you enter 1, You entered 2. when you enter 2, and You entered 3. when you enter 3.

For documentation, I dont expect you to add comment in each line, but you must explain the code appropriately

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!