Java statement output <identifier>

The point of this program is for the user to enter three exams, and their middle and letter class will return to them.

The way it is currently written gives me an error for the string "public static String getLetterGrade ..", and I don't know why this 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; } 
+6
source share
4 answers

It should be

  private static String getLetterGrade(int average){ 

or with any data type, and you are referencing another nonexistent variable in the switch statement intAverage

+3
source

I forgot to specify the type of the variable in the getLetterGrade method. Also correct this switch(intAverage/10) value to switch(average/10) .

0
source
 private static String getLetterGrade(int average) 

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

Switch

(intAverage / 10) must be changed to the switch (average / 10).

I also see some problems with your choice of int messing with precision if that is what you want to ignore. I would use if statements and range for switch enclosures, and not just drop them all to ints. Maybe it matters, maybe it isn't, but all that casting and loss of accuracy just makes me feel like the code is incomplete.

0
source

The average parameter has no type. It should be:

 private static String getLetterGrade(int average) { 

to match the type of variable you pass to it.

0
source

All Articles