Java switch statement with multiple cases.

Java 8Object Oriented ProgrammingProgramming

Following is the required program.

Example

Live Demo

public class Test {
   public static void main(String args[]) {
      // char grade = args[0].charAt(0); char grade = 'C'; switch(grade) {
         case 'A' : System.out.println("Excellent!"); break;
         case 'B' :
         case 'C' : System.out.println("Well done");
         break;
         case 'D' : System.out.println("You passed");
         case 'F' : System.out.println("Better try again");
         break;
         default : System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}

Output

Compile and run the above program using various command line arguments. This will produce the following result −

Well done
Your grade is C
raja
Published on 07-Feb-2018 10:02:16
Advertisements