Java switch语句<identifier> issue </identifier>

时间:2012-11-19 22:28:16

标签: java switch-statement identifier

该计划的目的是让用户输入三个考试成绩,并将其平均成绩和成绩归还给他们。

目前编写的方式给我一个错误,即公共静态字符串getLetterGrade ..&#39;我不知道为什么会这样......

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;

   }

4 个答案:

答案 0 :(得分:3)

应该是

 private static String getLetterGrade(int average){

或任何数据类型,并且您指的是switch语句intAverage中的另一个非存在变量

答案 1 :(得分:0)

您忘记在方法getLetterGrade中输入变量平均值的类型。同样将此switch(intAverage/10)更正为switch(average/10)

答案 2 :(得分:0)

private static String getLetterGrade(int average)

您忘记输入变量average的类型,我需要输入int类型。

switch(intAverage / 10)需要更改为switch(average / 10)。

除非你想忽略这一点,否则我也会看到你在选择int时遇到的一些问题。我将使用if语句和开关案例的范围,而不是仅将它们全部转换为整数。也许它有所作为也许它不会,但所有的铸造和精度的损失只会让我觉得代码不完整。

答案 3 :(得分:0)

参数average没有类型。它应该是:

private static String getLetterGrade(int average) {

匹配您传递给它的变量的类型。