Grade Calculator in Java: Calculate and Display Grades Based on Input Scores

 

Introduction

Grading is an important aspect of education, and this program helps you calculate and display the grade of a student based on their score. It takes the input score, checks it against predefined grade ranges, and returns the corresponding grade.
This is a simple implementation in Java that can be easily expanded or modified according to specific grading criteria.

Objective

The objective of this program is to take a score input from the user and display the corresponding grade based on predefined score ranges. The grades are typically:

  • 90-100: A
  • 80-89: B
  • 70-79: C
  • 60-69: D
  • Below 60: F

Java Code for Grade Calculator


import java.util.Scanner;

public class GradeCalculator {

    public static void main(String[] args) {
        // Create a scanner object to take user input
        Scanner scanner = new Scanner(System.in);

        // Prompt user to enter the score
        System.out.print("Enter your score: ");
        int score = scanner.nextInt();

        // Variable to store the grade
        char grade;

        // Calculate grade based on score
        if (score >= 90 && score <= 100) { grade = 'A'; } else if (score >= 80 && score < 90) { grade = 'B'; } else if (score >= 70 && score < 80) { grade = 'C'; } else if (score >= 60 && score < 70) {
            grade = 'D';
        } else {
            grade = 'F';
        }

        // Display the grade
        System.out.println("Your grade is: " + grade);

        // Close the scanner
        scanner.close();
    }
}
            

Explanation of the Program Structure

The program consists of the following components:

  1. Import Scanner: The Scanner class is used to get input from the user.
  2. Input Section: The program prompts the user to input a score. The input score is read using scanner.nextInt().
  3. Conditional Statements: The if-else if structure evaluates the score and assigns the correct grade.
  4. Grade Output: After calculating the grade, the program outputs the grade using System.out.println().

How to Run the Program

  1. Copy the provided Java code into a text file and save it as GradeCalculator.java.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where you saved GradeCalculator.java.
  4. Compile the Java program using the command:
    javac GradeCalculator.java
  5. Run the program by executing the following command:
    java GradeCalculator
  6. The program will prompt you to enter a score, and it will display the corresponding grade.
© 2025 Learn Programming. All Rights Reserved.

 

One Reply to “Grade Calculator in Java: Calculate and Display Grades Based on Input Scores”

Leave a Reply

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