user3330739
user3330739

Reputation: 23

How do I use a switch statement and get the correct output?

I am having trouble getting the output of this program to return the letter grade for the student. Also, I am having trouble with the max grade. Any help would be awesome.

   import java.util.Scanner;

   public class GPACalc {

    public static void main(String[] args) {

    Scanner input = new Scanner ( System.in );
    System.out.println( "Last Name: " );
    String studentName = input.next();

    System.out.println( "First Name: " );
    String firstName = input.next();

    System.out.println( "Enter CWID: " );
    int id = input.nextInt();

    System.out.print( "Enter Student Grades: " );

    int count = 0;
    double total = 0.000;
    double avg;
    int max = 0, min = 0;

    int gradeNum = 0;
    while ( true ) {
        int grade;
        grade = input.nextInt();
        if ( grade < 0.000 || grade > 100 )
            break;

        if ( grade > max );
        max = grade;

        if ( grade < min )
            ;
        min = grade;
        total = total + grade;
        count++;
    }
    avg = total / count;

    char letter = 'H';

    switch( letter ) {
    case 'A': 
        avg = 100;
        System.out.println("You have a perfect score. Keep up the excellent work!");
        break;
    case 'B':
        avg = 90;
        System.out.println("You have an A. Great Job!");
        break;
    case 'C':
        avg = 80;
        System.out.println("You have a B. Keep studying hard!");
        break;
    case 'D':
        avg = 70;
        System.out.println("You have a C. Commit a little more of your time.");
        break;
    case 'E':
        avg = 60;
        System.out.println("You have a D. You might want to study.");
        break;
    case 'F':
        avg = 50;
        System.out.println("You have an F. Better luck next semester.");
        break;
    case 'G':
        avg = 0;
        System.out.println( "It's time to change majors.");
        break;
    }


    System.out.println("\n----------------------------------------------------------");
    System.out.println( "    STUDENT   IFORMATION" );
    System.out.println("\n----------------------------------------------------------");
    System.out.println( "  Student Name  -- " + firstName + " " + studentName );
    System.out.println( "  Student ID    -- " + id );
    System.out.println( "  Students GPA  -- " + avg );
    System.out.println( "   # of Grades  -- " + count );
    System.out.println( "  Highest Grade -- " + max );
    System.out.println( "  Lowest Grade  -- " + min );
    System.out.println("\n----------------------------------------------------------");



    }

}

Upvotes: 1

Views: 3385

Answers (1)

cpurdy
cpurdy

Reputation: 1236

Your "if" statements won't work because you follow them with a semi-colon. You mean to do this:

if (a) dosomething;

... but instead you do this:

if (a) ; // does nothing
dosomething; // always does something regardless of "a"

Either you do it perfectly, or it won't work. There is no "A for effort".

Regarding your desire to use a switch statement, this is an easy one, but you've gone and tried to do it backwards. First you calculated the average. (That's a good idea.) Then you told the computer that the grade was an 'H'. (Why? That is obviously not a good idea.) Then you asked it to switch on the grade (which is an 'H') and if it's an 'H' you overwrite the average that you calculated with a zero. So all students will get zeros. And H's.

What you meant to do, was to take the average that you calculated, and get the grade letter from that. So switch on the average, and provide as case labels the values that the average could be:

switch (avg)
  {
  case 100: case 99: case 98: // etc.
    letter = 'A';
    break;
  // and so on
  }

Since you probably don't want to type "case 50" down to "case 0", just use a "default:" to match all the cases that you don't explicitly list.

p.s. make your avg into an int (not a double) so you can switch on it

p.p.s. print out the variables that you calculate so that you actually know that they are what you think they should be ... we call this "debugging" ;-)

Upvotes: 2

Related Questions