2012-11-19 35 views
6

該程序的重​​點是讓用戶輸入三個考試分數,並將其平均值和字母等級返回給他們。Java切換語句<identifier>問題

它目前正在編寫方式給我一個錯誤的「公共靜態字符串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; 

    } 
+0

您缺少參數「average」的數據類型 –

回答

3

應該

private static String getLetterGrade(int average){ 

或與任何數據類型,你指的是另一種非存在變量在switch語句intAverage

0

你忘了把變量平均的I型n方法getLetterGrade。還請將switch(intAverage/10)更正爲switch(average/10)

0
private static String getLetterGrade(int average) 

你忘了輸入變量average的類型,它需要類型int我承擔。

需要將交換機(intAverage/10)更改爲切換(平均/ 10)。

我也看到一些問題,你選擇int與準確性,除非那是你想忽略的東西。我會使用if語句和範圍來切換case,而不是僅僅將它們全部轉換爲int。也許它有所作爲也許它不會,但所有的投射和準確度的損失只是讓我覺得代碼是不完整的。

+0

average是一個int,而不是一個String。 – Maroun

0

參數average沒有類型。它應該是:

private static String getLetterGrade(int average) { 

以匹配您傳遞給它的變量的類型。

+0

他從不使用這個變量。我猜他必須在'switch'語句中使用'average'。 – Maroun