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");

Java Conditional Stmts

3 Likes

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");
}

Answered By

2 Likes


Related Questions