0
package Grade;

import java.util.Scanner;

public class grade {
    public static void main(String[] args){

        System.out.println("Welcome to the Letter Grade Converter");
        System.out.println();
        
        Scanner sc = new Scanner(System.in);
        
        String choice = "y";
        while (choice.equalsIgnoreCase("y")){
            
            System.out.println("Enter numerical grade: ");
            int grade = sc.nextInt();

            char letter = 0;

            if(grade <= 100 || grade >= 88)
                letter = 'A';
                System.out.println("Your Grade is: " + letter);

            if (grade <= 87 || grade >= 80)
                letter = 'B';
                System.out.println("Your Grade is: " + letter);
            
            if (grade <= 79 || grade >= 67)
                letter = 'C';
                System.out.println("Your Grade is: " + letter);
            

            if(grade <= 66 || grade >= 60)
                letter = 'D';
                System.out.println("Your Grade is: " + letter);
            

            if(grade <= 60)
                letter = 'F';
                System.out.println("Your Grade is: " + letter);

            System.out.println("Continue? (y/n): ");
            choice = sc.next();
            System.out.println();

        }

    }

}

[OUTPUT]
Welcome to the Letter Grade Converter

Enter numerical grade:
90
Your Grade is: A
Your Grade is: B
Your Grade is: C
Your Grade is: D
Your Grade is: D
Continue? (y/n):
n

BUILD SUCCESSFUL
[/OUTPUT]

I'm trying to get only one letter grade, plus instead of 'F' I'm getting 'D'.
Any help would be really appreciated.
Thanks

3
Contributors
3
Replies
7
Views
6 Years
Discussion Span
Last Post by f_atencia
0

Use a switch statement instead:
switch(grade)
{
case(grade<100 && grade>90):System.out.println("Your grade is A"); break;
case(grade<90 && grade >80):System.out.println("Your grade is B"); break;
...
default:System.out.println("Failed");
}
Essentially, a switch statement grabs a variable (in this case grade) and compares it against numerous cases. If a case is met, it will run the code after the : character. In no other cases are met, it will run the default code (in this case, fail). The break will move to match the grade to the next case, so make sure that is included. This is much better than having numerous if-else statements. Hope this helps

Edited by f_atencia: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.