user1837129
user1837129

Reputation: 61

Java switch statement <identifier> issue

The point of this program is for a user to enter three exam scores and their average and letter grade is returned to them.

The way it is currently written gives me an error for the 'public static String getLetterGrade..' line and I don't know why that is..

public class GradeProblem
{
public static void main(String[] args)
{
 char letterGrade;
 String exam1, exam2, exam3;
 double exam1Score, exam2Score, exam3Score, average; 

 exam1 = JOptionPane.showInputDialog(null, "Enter your score for Exam 1: ");
 exam1Score = Double.parseDouble(exam1.substring(0,2));
 int intExam1Score = (int)exam1Score;

 exam2 = JOptionPane.showInputDialog(null, "Enter your score for Exam 2: ");
 exam2Score = Double.parseDouble(exam2.substring(0,2));
 int intExam2Score = (int)exam2Score;

 exam3 = JOptionPane.showInputDialog(null, "Enter your score for Exam 3: ");
 exam3Score = Double.parseDouble(exam3.substring(0,2));
 int intExam3Score = (int)exam3Score;

 average = (intExam1Score + intExam2Score + intExam3Score) / 3;

 int intAvergage = (int)average;
 letterGrade = getLetterGrade(intAverage);

 System.out.println("Your average is "+average);  
 System.out.println("Your letter grade is "+letterGrade); 

 }

 private static String getLetterGrade(average)
 {
String letterGrade;
switch(intAverage/10)
{
    case 10: letterGrade = "A";
    case 9: letterGrade = "A";
              break;
    case 8: letterGrade = "B";
              break;
    case 7: letterGrade = "C";
              break;
    case 6: letterGrade = "D";
    default:
              letterGrade = "E";
}
return letterGrade;

   }

Upvotes: 6

Views: 438

Answers (3)

Jeff
Jeff

Reputation: 180

private static String getLetterGrade(int average)

You forgot to enter the type of variable average, it needs to be type int I assume.

switch(intAverage/10) needs to be changed to switch(average/10).

I'm also seeing some problems with your choice of int messing with accuracy unless that's something you want to ignore. I'd use if statements and a range for the switch cases, rather than just casting them all to ints. Maybe it makes a difference maybe it doesn't but all that casting and loss of accuracy just makes me feel like the code is incomplete.

Upvotes: 0

Bohemian
Bohemian

Reputation: 425288

The parameter average has no type. It should be:

private static String getLetterGrade(int average) {

to match the type of the variable you're passing to it.

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240946

It should be

 private static String getLetterGrade(int average){

or with any datatype, and you are referring to another non exist variable in switch statement intAverage

Upvotes: 3

Related Questions