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:
- Import Scanner: The
Scanner
class is used to get input from the user. - Input Section: The program prompts the user to input a score. The input score is read using
scanner.nextInt()
. - Conditional Statements: The
if-else if
structure evaluates the score and assigns the correct grade. - Grade Output: After calculating the grade, the program outputs the grade using
System.out.println()
.
How to Run the Program
- Copy the provided Java code into a text file and save it as
GradeCalculator.java
. - Open a terminal or command prompt.
- Navigate to the directory where you saved
GradeCalculator.java
. - Compile the Java program using the command:
javac GradeCalculator.java
- Run the program by executing the following command:
java GradeCalculator
- The program will prompt you to enter a score, and it will display the corresponding grade.