1

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);

    }

}
Memori Ruffin
  • 25
  • 1
  • 5
  • In addition that doesn't work correctly. It doesn't print out the letter grade. – Memori Ruffin Oct 07 '15 at 18:53
  • Consider adding any additional comments for your own question directly to the question via the "Edit" link underneath it. – user3071284 Oct 07 '15 at 19:01
  • May be a duplicate but not positive. I'm sure some of the answers there could be your solution: [In Java,Using switch statement with a range of value in each case?](http://stackoverflow.com/questions/10873590/in-java-using-switch-statement-with-a-range-of-value-in-each-case) – CubeJockey Oct 07 '15 at 19:13
  • This may be different because I'm trying to get my program to print out a letter grade (String or Char), by what ever number grade the user puts in the system( double). – Memori Ruffin Oct 07 '15 at 19:19

3 Answers3

1

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.

Austin
  • 4,801
  • 6
  • 34
  • 54
0

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();
Keith
  • 3,079
  • 2
  • 17
  • 26
0

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";
}
Rabbit Guy
  • 1,840
  • 3
  • 18
  • 28