Build a program that reads a numeric score and prints the letter grade. Use comparison operators, logical AND, and the ternary operator.
import java.util.Scanner;
public class GradeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your score (0-100): ");
int score = scanner.nextInt();
String grade;
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else if (score >= 60) {
grade = "D";
} else {
grade = "F";
}
String status = (score >= 60) ? "Passing" : "Failing";
System.out.println("Grade: " + grade + " (" + status + ")");
scanner.close();
}
}
Notice how each else if only needs one comparison. Since the checks run top to bottom, reaching score >= 80 already means score < 90.