Write a Java Program Find out Students Grades using Switch Case

Java Program to find the grade of a student using switch case. Here we will write a Java program find out students grades using switch case statement. The below table shows the grading system.

Score in subjectGrade
>=90A
80-89B
70-79C
60-69D
50-59E
<50F

The switch case statement in Java programming is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly. Two keywords are used in the program:- switch and case.

public class Student {

   public static void main(String[] args) {
      // score (0-100)
      int score = 79;
      String grade = null;
      
      switch(score/10) {
        // for >= 90
        case 10:
        case 9:
           grade = "A";
           break;
        // for >= 80 and <90
        case 8:
           grade = "B";
           break;
        // for >= 70 and <80
        case 7:
           grade = "C";
           break;
        // for >= 60 and <70
        case 6:
           grade = "D";
           break;
        // for >= 50 and <60
        case 5:
           grade = "E";
           break;
        // for < 50
        default:
           grade = "F";
           break;
      }
      
      // display result
      System.out.println("Grade = " + grade);
   }

}

Output:-

Grade = C

In this example, we had taken score value directly and finding the grade based on that score. Since it is checking grade only for the 1 subject therefore the score must be within 0-100 range. The break keyword is mandatory else we will always get grade = F.

Java Program Find Students Grades using Switch Case and Methods

Now, let us develop an another Java program with the help of methods. The score value will be taken from the end-user as input and the logic to find the grade will be written in seperate method. In main method we will call them and get the result.

import java.util.Scanner;

public class Student {
   
   public static String findGrade(int score) {
      // check score is within 0-100 or not
      if(score < 0 || score > 100)
         return "Invalid score"; // or throw exception
      
      // switch-case
      switch(score/10) {
        // for >= 90
        case 10:
        case 9:
           return "A";
        // for >= 80 and <90
        case 8:
           return "B";
        // for >= 70 and <80
        case 7:
           return "C";
        // for >= 60 and <70
        case 6:
           return "D";
        // for >= 50 and <60
        case 5:
           return "E";
        // for < 50
        default:
           return "F";
      }
   }

   public static void main(String[] args) {
      // Create Scanner class object to get input value
      Scanner scan = new Scanner(System.in);
      
      // take input for score
      System.out.print("Enter score value: ");
      int score = scan.nextInt();
      
      // find grade and display result
      System.out.println("Grade = " + findGrade(score));
      
      // close Scanner 
      scan.close();
   }

}

Output:-

Enter score value: 100
Grade = A

Enter score value: 92
Grade = A

Enter score value: 79
Grade = C

Enter score value: 45
Grade = F

Enter score value: 115
Grade = Invalid score

In this program, we didn’t use the break statement. Whenever the condition is satisfied then the string value is returned and the control came back to the main method. The remaining logic after the execution of that return statement won’t be executed. But if the conditions are not satisfied then the default block will be executed and it returns “F” grade. For the score less than 0, and greater than 100 we are returning “Invalid Score” but it is better to throw an exception.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *