2015-09-14 89 views
-1

所以我想創建一個列表而不創建數組列表,但是,每當我嘗試運行程序時,我得到的錯誤「局部變量」letterGrade「可能尚未初始化」我試圖更改整個代碼但我仍然有同樣的問題>。 <任何幫助將不勝感激。先謝謝你。本地變量可能沒有被初始化的問題?


import java.util.Scanner; 

public class KifahProg4 
{ 
public static void main(String[] args) 
{ 
Scanner stdIn = new Scanner(System.in); 

String lastName,firstName,fullName,list; 
int score,countA,countB,countC,countD,countF; //Student scores and number of Grades 
int total = 0; //Student total score 
double average; //Student Average score 
char letterGrade; //Student grade 
char response; //User continue y/n 


do 
{ 
    System.out.print("Enter the student last name: "); 
    lastName = stdIn.next(); 
    System.out.print("Enter the student first name: "); 
    firstName = stdIn.next(); 
    fullName = lastName + " " + firstName; 
    for (int i=0; i<=5; i++) 
    { 
    System.out.print("Enter the score: "); 
    score = stdIn.nextInt(); 
    total += score; 
    average = (double) total/5; 

    if(average >= 90 && average <=100) 
    { 
     letterGrade = 'A'; 
    } 
    else if (average >=80) 
    { 
     letterGrade = 'B'; 
    } 
    else if (average >=70) 
    { 
     letterGrade = 'C'; 
    } 
    else if (average >= 60) 
    { 
     letterGrade = 'D'; 
    } 
    else if (average <= 59) 
    { 
     letterGrade = 'F'; 
    } 
    System.out.println("Grade is: "); 

    list = fullName + "\t" + total + average + "\t" + letterGrade + "\n"; 
    } 
    System.out.println(
     "Would you like to add another student? (y/n): "); 
    response = stdIn.next().charAt(0); 
} while (response == 'y' || response == 'Y'); 

} //結束主 } //結束類KifahProg4

+0

我忘了提。我在(list = fullname +「\ t」+ total等...)行上得到錯誤 – Kifah

+0

如果我把分數設置爲101,該怎麼辦? – immibis

回答

1

設置letterGrade一些默認值,你使用它之前擺脫編譯器警告的你得到。

例如

char letterGrade = 'Z'; 

如果仍設置爲「Z」,當你到你知道你有一個錯誤條件list = fullName + "\t" + total + average + "\t" + letterGrade + "\n"; ...

相關問題