2015-09-14 51 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"; ...

相关问题