Java Program to Calculate Student Grade using Switch Case

In this Java tutorial, we will learn How to develop a Java program to Calculate and Display Student Grades. We will also understand the Working Logic behind the Code. We will Learn to write a Java program-Calculate student grade using Switch-Case. So, Let’s get started –

Java Program To Calculate Student Grade with Switch Case


Things, You should know –

Before starting the Solution of this Java program to Calculate Student Grade using Switch-Cases, it would be better if you have some basic knowledge of these Core Java Concepts

  • Defining variables and Array,
  • Switch – Case syntax,
  • Loops (like for / while)
  • Taking input from console (using Scanner class)

1) Java Program: Calculate Student Grade with Switch Case

To calculate Student Grade in real life, think about the steps you would follow to calculate the grade. As we learned in our previous tutorial- Java program to Calculate and Display Student Grades using if-else, we have some major steps as – Taking marks as input, processing average and then calculate it’s grade to print in console/terminal.

Here, in this case, it depends on the same but with ‘switch-case‘ workflow, If you are not familiar with Java ‘switch-case‘ syntax and behavior, then take this :

Syntax :

 

Key points of switch-case :

  1. We can put either char or int inside the switch( ) parenthesis.
  2. To exit from the switch statement, use the break keyword.
  3. It’s not necessary to have a default case in every switch-case.
  4. In the above syntactical example, if calculateExpression == valueA, then block-A would be executed, if calculateExpression == valueB, then block-B would be executed, and so on.
  5. If calculateExpression doesn’t get match with any value, then the default case would be executed.
  6. Note*** If we did not put the break keyword at the end of cases, JVM would not throw any error or Exception.
    Assume, if calculateExpression == valueB and block-B doesn’t have any break statement, then the compiler would execute all the next blocks until it reaches to any break statement or executes the last block – the default case block.

In this program, we will solve the problem using the switch-case syntax to Calculate the Student’s Grade.

 

Solution :

import java.util.Scanner;

/*
      Java Program : To calculate Grade for a Student using Switch-Case
      By - ShubhamKLogic.com  || Visit for more Interactive Java Programs
*/

public class StudentGradeCalculate1 {                                           // Explaining : Step-by-Step

    public static void main(String[] theArgs) {                               // main() Method Started

        final int numberOfSubjects = 5;                                               // To store number of Subjects
        int theMarks[] = new int[numberOfSubjects];                      // To store Marks of student
        char theGrades[] = new char[numberOfSubjects];               // To store calculated Grades
        int i;


        Scanner scanner = new Scanner(System.in);                        // To take Input from User on Console/Terminal
                                                                                                                    // Now, Printing the message for better understanding

        System.out.print("\n***********************************");
        System.out.print("\n***********************************");
        System.out.print("\n********  Student Grade  **********");
        System.out.print("\n*****  Calculator Program  ********");
        System.out.print("\n***********************************");
        System.out.print("\n***********************************\n\n");


        System.out.print("\n***********************************");
        System.out.print("\n*******  Total Subjects: 5 ********");
        System.out.print("\n********  Max Marks : 100 *********");
        System.out.print("\n***********************************\n\n");


        System.out.println("Enter the Marks of :\n");                        // Running a loop to input Marks of all Subjects
        for (i = 0; i < numberOfSubjects; i++) {

            System.out.print("Subject (" + (i + 1) + ") : ");
            int marks = scanner.nextInt();

            if (marks < 0 || marks > 100) {                     // Checking, if it's a valid mark or not

                System.out.println("\n[WARNING] Invalid Marks : Enter Marks in the Range 0-100 \n");
                i--;                                                                 // Decrementing counter i as this iteration got waste, so, Recover the previous count.

            } else {

                theMarks[i] = marks;                                // Store the valid marks

                switch (marks / 10) {                                // Calculating the Grade
                    case 10:
                    case 9:
                        // If Marks are in between 90-100 
                        theGrades[i] = 'A';                               // Storing the Grade 
                        break;
                    case 8:
                    case 7:
                        // If Marks are in between 70-89
                        theGrades[i] = 'B';
                        break;
                    case 6:
                        // If Marks are in between 60-69
                        theGrades[i] = 'C';
                        break;
                    case 5:
                    case 4:
                        // If Marks are in between 40-59
                        theGrades[i] = 'D';
                        break;
                    default:
                        // If Marks are in between 0-39
                        theGrades[i] = 'E';
                }
            }
        }

        scanner.close();                                                      // All Input done, so closing the Scanner
                                                                                          // Displaying the Results : Grades
        System.out.print("\n***********************************");
        System.out.print("\n***********************************");
        System.out.print("\n********  Report Summary  *********");
        System.out.print("\n***********************************");
        System.out.print("\n***********************************");
        System.out.print("\n     Marks      |     Grade        ");
        System.out.print("\n***********************************");

        for (i = 0; i < theMarks.length; i++)
            System.out.print("\n      " + theMarks[i] + "        |       " + theGrades[i]);

        System.out.print("\n***********************************");
    } // Program Ended ...
}

 

Output :

When you’ll run the above Student Grade Calculator Java program, you’ll successfully get the following results –

***********************************
***********************************
********  Student Grade  **********
*****  Calculator Program  ********
***********************************
***********************************


***********************************
*******  Total Subjects: 5 **********
********  Max Marks : 100 *********
***********************************

Enter the Marks of :

Subject (1) : 88
Subject (2) : 77
Subject (3) : 88
Subject (4) : 99
Subject (5) : 55

***********************************
***********************************
*******  Report Summary  *********
***********************************
***********************************
     Marks  |     Grade
***********************************
      88        |       B
      77        |       B
      88        |       B
      99        |       A
      55        |       D
***********************************

Steps & Logic : Behind the Code

The logic of the above Java program-Calculate Student Grade using switch-case is pretty simple :

[Step 1] First, we have set and defined the numberOfSubjects variable, declared theMarks and theGrades array.

[Step 2] Then, we created the Scanner object to take input marks from the user from console/terminal.

[Step 3] After that, we ran a loop till the numberOfSubjects and followed the Step 4 to 6.

[Step 4] We validated theMarks, whether it’s less than zero or greater than 100.

[Step 5] If the marks are validated, Then we stored them in theMarks array.

[Step 6] Inside the switch-case, we calculated theGrades[ ].

[Step 7] Then, we closed the scanner and successfully printed the results.


2) Java Program to Calculate Student Grade : Switch-Case

Till now, we have seen the Grade calculation Java program only of one student. But you can modify the above solution to accept Marks of N students and Print their Grades till the user wants. Here is the Java program to do it –

Solution :

import java.util.Scanner;

/*
      Java Program : To calculate Grade for N Students using Switch-Case
      By - ShubhamKLogic.com  || Visit for more Interactive Java Tutorials
*/

public class StudentGradeCalculate2 {                                          // Explaining : Step-by-Step

    public static void main(String[] theArgs) {

        final int numberOfSubjects = 5;                                               // To store number of Subjects
        int theMarks[] = new int[numberOfSubjects];                     // To store Marks of student
        char theGrades[] = new char[numberOfSubjects];             // To store calculated Grades
        int i,k=0;
        boolean isMoreStudents = true;
        
        Scanner scanner = new Scanner(System.in);                  // To take Input from User on Console/Terminal
                                                                                                                   // Now, Printing the message for better understanding

        System.out.print("\n***********************************");
        System.out.print("\n***********************************");
        System.out.print("\n********  Student Grade  **********");
        System.out.print("\n*****  Calculator Program  ********");
        System.out.print("\n***********************************");
        System.out.print("\n***********************************\n\n");


        System.out.print("\n***********************************");
        System.out.print("\n*******  Total Subjects: 5 ********");
        System.out.print("\n********  Max Marks : 100 *********");
        System.out.print("\n***********************************\n\n");

        while(isMoreStudents)
        {
            System.out.print("\n***********************************\n");
            System.out.print(  "******  Details: Student "+ ++k +"  *******\n");
            System.out.print(  "***********************************\n\n");
            System.out.println("Enter the Marks of :\n");             // Running a loop to input Marks of all Subjects
            for (i = 0; i < numberOfSubjects; i++) {

                System.out.print("Subject (" + (i + 1) + ") : ");
                int marks = scanner.nextInt();

                if (marks < 0 || marks > 100) {                                    // Checking, if it's a valid mark or not
 
                    System.out.println("\n[WARNING] Invalid Marks : Enter Marks in the Range 0-100 \n");
                    i--;                                                                                   // Decrementing counter i as this iteration got waste, so, Recover the previous count.

                } else {

                    theMarks[i] = marks;                                                 // Store the valid marks

                    switch (marks / 10) {                                               // Calculating the Grade
                        case 10:
                        case 9:
                            // If Marks are in between 90-100 
                            theGrades[i] = 'A';                                              // Storing the Grade 
                            break;
                        case 8:
                        case 7:
                            // If Marks are in between 70-89
                            theGrades[i] = 'B';
                            break;
                        case 6:
                            // If Marks are in between 60-69
                            theGrades[i] = 'C';
                            break;
                        case 5:
                        case 4:
                            // If Marks are in between 40-59
                            theGrades[i] = 'D';
                            break;
                        default:
                            // If Marks are in between 0-39
                            theGrades[i] = 'E';
                    }
                }
            }

                                                                                                        // Displaying the Results : Grades
            System.out.print("\n***********************************");
            System.out.print("\n***********************************");
            System.out.print("\n********  Report Summary  *********");
            System.out.print("\n***********************************");
            System.out.print("\n***********************************");
            System.out.print("\n     Marks      |     Grade        ");
            System.out.print("\n***********************************");

            for (i = 0; i < theMarks.length; i++)
                System.out.print("\n      " + theMarks[i] + "        |       " + theGrades[i]);

            System.out.print("\n***********************************\n\n");
            
            System.out.print("Have More Students Details? (Yes->1 / No->0) || Enter Key : ");
            
            if( scanner.next().charAt(0) == '0'){
                System.out.print("\n***********************************");
                System.out.print("\n********  Total Report: "+ k +" *********");
                System.out.print("\n***********************************");
                scanner.close();
                break;                                                                              // Getting out of the while{} loop
            }
        }
    }                                                                                                      // Program Ended ...
}

 

Output :

When you’ll run the above program, you’ll successfully get similar results –

***********************************
***********************************
********  Student Grade  **********
*****  Calculator Program  ********
***********************************
***********************************


***********************************
*******  Total Subjects: 5 ********
********  Max Marks : 100 *********
***********************************


***********************************
******  Details: Student 1  *******
***********************************

Enter the Marks of :

Subject (1) : 99
Subject (2) : 88
Subject (3) : 90
Subject (4) : 80
Subject (5) : 85

***********************************
***********************************
********  Report Summary  *********
***********************************
***********************************
     Marks      |     Grade
***********************************
      99        |       A
      88        |       B
      90        |       A
      80        |       B
      85        |       B
***********************************

Have More Students Details? (Yes->1 / No->0) || Enter Key : 1

***********************************
******  Details: Student 2  *******
***********************************

Enter the Marks of :

Subject (1) : 33
Subject (2) : 23
Subject (3) : 43
Subject (4) : 23
Subject (5) : 33

***********************************
***********************************
********  Report Summary  *********
***********************************
***********************************
     Marks      |     Grade
***********************************
      33        |       E
      23        |       E
      43        |       D
      23        |       E
      33        |       E
***********************************

Have More Students Details? (Yes->1 / No->0) || Enter Key : 1

***********************************
******  Details: Student 3  *******
***********************************

Enter the Marks of :

Subject (1) : 66
Subject (2) : 56
Subject (3) : 45
Subject (4) : 76
Subject (5) : 86

***********************************
***********************************
********  Report Summary  *********
***********************************
***********************************
     Marks      |     Grade
***********************************
      66        |       C
      56        |       D
      45        |       D
      76        |       B
      86        |       B
***********************************

Have More Students Details? (Yes->1 / No->0) || Enter Key : 0

***********************************
********  Total Report: 3 *********
***********************************

Wrap Up 

So far, we have learned How to develop the Java program to Calculate Grade from 1 student to N number of students using Switch-Case. We have also understood the step-by-step working logic behind the code.

I hope, this article helped you to learn a bit more about Java Arithmetics, Calculations and Conditional switch-case concepts. If you have any doubt or query, then do comment or send me a direct email via Google, I’ll try my best to give you the solution within a day.

Also, Rate this article on Facebook and, Don’t forget to check Other Useful Java Articles that you may like :

Read Next :

Leave a Comment