Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

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 107 views
1 answer 66 views
1 answer 75 views
1 answer 83 views
2 answers 99 views
2 answers 87 views
...