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

Course Queries Syllabus Queries 3 years ago

5.66K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago

 

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
                                                
0 views
0 shares

profilepic.png
manpreet 3 years ago

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.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum