Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java开关语句<;标识符>;问题_Java_Switch Statement_Identifier - Fatal编程技术网

Java开关语句<;标识符>;问题

Java开关语句<;标识符>;问题,java,switch-statement,identifier,Java,Switch Statement,Identifier,这个程序的目的是让用户输入三个考试分数,然后将他们的平均分数和字母分数返回给他们 当前的编写方式为“publicstaticstringgetletlettergrade..”行提供了一个错误,我不知道为什么会这样 public class GradeProblem { public static void main(String[] args) { char letterGrade; String exam1, exam2, exam3; double exam1Score, exam2

这个程序的目的是让用户输入三个考试分数,然后将他们的平均分数和字母分数返回给他们

当前的编写方式为“publicstaticstringgetletlettergrade..”行提供了一个错误,我不知道为什么会这样

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;

   }
应该是

 private static String getLetterGrade(int average){
或者使用任何数据类型,并且您在switch语句
intAverage

private static String getLetterGrade(int average)
您忘记输入变量
average
的类型,我假定它必须是
int
类型

开关(平均值/10)需要更改为开关(平均值/10)


我也看到了一些问题,你的整数选择与准确性混乱,除非这是你想忽略的东西。我会使用if语句和switch case的范围,而不是将它们全部转换为int。也许这会有所不同,也许不会,但所有的铸造和精度的损失让我觉得代码是不完整的。

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

private static String getLetterGrade(int average) {

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

您缺少参数“average”的数据类型average是一个int,而不是字符串。他从不使用此变量。我猜他必须在
switch
语句中使用
average