How to get a grade and transform it into a letter grade in Java

1 Answer

0 votes
public class Program {
    public String toLetterGrade(double score) {
        // Define scores and grades
        double[] scores = {95.0, 90.0, 85.0, 80.0, 75.0, 70.0, 65.0, 60.0};
        String[] grades = {"A+", "A", "B+", "B", "C+", "C", "D+", "D"};

        // Iterate through scores and find the grade
        int scores_length = scores.length;
        for (int i = 0; i < scores_length; i++) {
            if (score >= scores[i]) {
                return grades[i];
            }
        }

        return "F"; // Default grade if none of the scores match
    }

    public static void main(String[] args) {
        Program program = new Program();

        // Test the program with individual scores
        System.out.println(program.toLetterGrade(95)); // A+
        System.out.println(program.toLetterGrade(90)); // A
        System.out.println(program.toLetterGrade(80)); // B
        System.out.println(program.toLetterGrade(60)); // D
        System.out.println(program.toLetterGrade(50)); // F
    }
}


/*
run:

A+
A
B
D
F

*/

 



answered Mar 23, 2025 by avibootz
edited Mar 23, 2025 by avibootz

Related questions

2 answers 130 views
1 answer 82 views
1 answer 90 views
1 answer 96 views
2 answers 135 views
2 answers 106 views
...