Memori Ruffin
Memori Ruffin

Reputation: 25

Switch statement to determine letter grade from user input (decimal)

My instructor wants me to use a switch statement, but I'm calculating the grade of a student and by nature I would think to use double but I can't. The program should ask for user input then by which ever number (example: 92.99) the student enters, print out a letter grade according to the syllabus. Please help! In addition that doesn't work correctly anymore. It doesn't print out the letter grade. This is what I have so far:

public class calculate {

    private static String gradefinal;
    private static char grade;

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
    // variable declaration

        Scanner in = new Scanner(System.in);    //Input device scanner
        double finalgrade = 0.0; 

    // input 

        String first_name;              //Holds first name 
        System.out.print("Enter your first name: ");
        first_name = in.next();

        String last_name;               //Holds last name 
        System.out.print("Enter your last name: ");
        last_name = in.next();

        String major;                   //Holds user's major
        System.out.print("Enter your intended major: ");
        major = in.next();

        String ist;                     //Holds user's class number
        System.out.print("Enter the IST class you are in: ");
        ist = in.next();

        String final_grade;         //Holds user's letter grade
        System.out.print("Enter your final grade: ");
        final_grade = in.next();

        // process 



        if (finalgrade <= 59.99) 
        {
            grade = 'F';

        }
        else if (finalgrade >= 90.00)
        {
            grade = 'A';
        }
        else if ((finalgrade <= 89.99) && (finalgrade >= 79.99))
        {
            grade = 'B';
        }
        else if ((finalgrade <= 79.99) && (finalgrade >= 69.99))
        {
            grade = 'C';
        }   
        else if ((finalgrade <= 69.99) && (finalgrade >= 60.00))
        {
            grade = 'D';
        }

    //output    
        System.out.printf("First Name: %s\nLast Name: %s\nIntended Major: %s\nIST Class: %s\nYour final grade is: %s\nfinal letter grade: %s",first_name, last_name, major, ist, final_grade, grade);

    }

}

Upvotes: 1

Views: 2686

Answers (3)

Rabbit Guy
Rabbit Guy

Reputation: 1892

Round it to the nearest 10s place, turn it to a string, then switch on the string:

String gradePercent = Math.round(finalGrade/10).toString();
switch (gradePercent) {
    "6" : grade = "D"; break;
    "7" : grade = "C"; break;
    "8" : grade = "B"; break;
    "9" : "10" : grade = "A"; break;
    default: grade = "F";
}

Upvotes: 0

Austin
Austin

Reputation: 4929

Alright this is homework so I'll keep the code to a minimum.

I think there's a few options here, and it all depends on what you're allowed to use.

1) The easiest option I would think is to "convert" the double to a String, and use a switch statement with the first character of the String. So if it starts with a 9, it's going to be an A, since you aren't calculating A+/A- it seems.

2) You could round down to the nearest tens place, and cast the double to an int, and use the switch statement with an int. That way you only have 4 numbers to check. The default will be your friend here.

Upvotes: 1

Keith
Keith

Reputation: 3151

As you have learned, a double cannot be used in a switch.

The Switch Statement

Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).

One option is to autobox a double into a Double and then get its int value:

Integer i = d.intValue();

Upvotes: 0

Related Questions