Switch Statement

Switch Statement

Open the provided file SwitchErrors.java. The Java source code is shown below. The program evaluates an integer entered by the user and displays the color assigned to the integer. Compile the program. The program has several syntax and logic errors. Fix the syntax errors and compile and run the program. Does the program run as you expected? Locate and correct the errors in the program logic. Be sure to make use of the debugger available in Eclipse to help identify errors.

package edu.ilstu;

import java.util.Scanner;

public class SwitchErrors {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.out);

System.out.println("Key: 1=blue, 2=red, 3=green");
System.out.print("Enter a number and I'll return ");
System.out.print(" the corresponding color. ");
number = keyboard.nextInt();

switch(number) {
case 1:
System.out.println("You chose red!");
case 2:
System.out.println("You chose blue!");
case 3:
System.out.println("You chose green!");
System.out.println("Color not available!");

Powered by