ProAnswers.org

How to write The program by using switch Statement in java?

public class MainClass {
public static void main(String[] args) {
int choice = 2;

switch (choice) {
case 1:
  System.out.println("Choice 1 selected");
  break;
case 2:
  System.out.println("Choice 2 selected");
  break;
case 3:
  System.out.println("Choice 3 selected");
  break;
default:
  System.out.println("Default");
  break;
}

}
}

public class MainClass {

public static void main(String[] args) {
char yesNo = ā€˜Nā€™;

switch(yesNo) {
  case 'n': case 'N':
       System.out.println("No selected");
       break;
  case 'y': case 'Y':
       System.out.println("Yes selected");
       break;
}

}
}
output
No selected