Computer Applications
Rewrite the following if statement, using the switch statement:
if (choice == 1)
System.out.println("You selected One");
else if (choice == 2)
System.out.println("You selected Two");
else if (choice == 3)
System.out.println("You selected Three");
else if (choice == 4)
System.out.println("You selected Four");
else if (choice == 5)
System.out.println("You selected Five");
else if (choice == 6)
System.out.println("You selected Six");
else
System.out.println("Invalid choice");
if (choice == 1)
System.out.println("You selected One");
else if (choice == 2)
System.out.println("You selected Two");
else if (choice == 3)
System.out.println("You selected Three");
else if (choice == 4)
System.out.println("You selected Four");
else if (choice == 5)
System.out.println("You selected Five");
else if (choice == 6)
System.out.println("You selected Six");
else
System.out.println("Invalid choice");
Answer
switch (choice) {
case 1:
System.out.println("You selected One");
break;
case 2:
System.out.println("You selected Two");
break;
case 3:
System.out.println("You selected Three");
break;
case 4:
System.out.println("You selected Four");
break;
case 5:
System.out.println("You selected Five");
break;
case 6:
System.out.println("You selected Six");
break;
default:
System.out.println("Invalid choice");
}
Related Questions
Write the following switch statement by using nested if statements:
switch (choice) { case 0: case 1: x = 111; y = 222; break; case 2: x = 333; y = 444; break; case 3: x = -11; y = -22; break; default: y = 555; }
Write an if statement to find the smallest of the three given integers using the min() method of the Math class.
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;}Format the following if statements with indentation:if (x == y) {if (y == z) x = 1; y = 2; } else z = 1;