Computer Applications
Rewrite the following if statement, using the switch statement:
if (choice == 0)
System.out.println("You selected Blue");
else if (choice == 1)
System.out.println("You selected Cyan");
else if (choice == 2)
System.out.println("You selected Red");
else if (choice == 3)
System.out.println("You selected Magenta");
else if (choice == 4)
System.out.println("You selected Green");
else if (choice == 5)
System.out.println("You selected Yellow");
else
System.out.println("Invalid choice");
Answer
switch (choice) {
case 1:
System.out.println("You selected Blue");
break;
case 2:
System.out.println("You selected Cyan");
break;
case 3:
System.out.println("You selected Red");
break;
case 4:
System.out.println("You selected Magenta");
break;
case 5:
System.out.println("You selected Green");
break;
case 6:
System.out.println("You selected Yellow");
break;
default:
System.out.println("Invalid choice");
}
Related Questions
Format the following if statements with indentation:if (x == y) {if (y == z) x = 1; y = 2; } else z = 1;
Format the following if statements with indentation:if (num1 != num2) {
if (num2 >= num3) x = 1; y = 2; }
else {x = 1; if (num1 == num2) z = 3;}Write the following switch statement by using nested if statements:
switch (choice) { case 0: case 1: x = 11; y = 22; break; case 2: x = 33; y = 44; break; default: y = 55; break; }Write an if statement to find the smallest of the three given integers using the min() method of the Math class.