6

该程序的重​​点是让用户输入三个考试成绩,然后将他们的平均成绩和字母成绩返回给他们。

它当前的编写方式给了我一个错误的“公共静态字符串 getLetterGrade..”行,我不知道为什么会这样..

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

3 回答 3

3

它应该是

 private static String getLetterGrade(int average){

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

于 2012-11-19T22:29:57.237 回答
0
private static String getLetterGrade(int average)

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

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

我还发现您选择 int 会导致准确性出现一些问题,除非您想忽略这一点。我会使用 if 语句和 switch 案例的范围,而不是仅仅将它们全部转换为整数。也许它会有所作为,也许它不会,但所有的铸造和准确性的损失只是让我觉得代码不完整。

于 2012-11-19T22:30:50.647 回答
0

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

private static String getLetterGrade(int average) {

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

于 2012-11-19T22:31:11.593 回答