我已经初始化并声明了变量“平均值”。我不明白为什么它不会编译。这是否与它在 if else 中的事实有关?我已经尝试过“|| null”,但也没有。该怎么办?
// declare variables
Scanner keyboard = new Scanner (System.in);
String given;
String middle;
String sur;
String truefalse;
boolean bool;
int exam1;
int exam2;
int exam3;
double average;
// get input
System.out.println("*************** Grade Computer *************");
System.out.println("Enter the student's first name: ");
given = keyboard.nextLine();
System.out.println( "Enter the student's middle initial: ");
middle = keyboard.nextLine();
System.out.println( "Enter the student's last name: ");
sur = keyboard.nextLine();
System.out.println( "Enter EXAM 1 Grade: ");
exam1 = keyboard.nextInt();
System.out.println( "Enter EXAM 2 Grade: ");
exam2 = keyboard.nextInt();
System.out.println( "Enter EXAM 3 Grade: ");
exam3 = keyboard.nextInt();
keyboard.nextLine();
System.out.println( "Bonus work completed [true/false]");
truefalse = keyboard.nextLine();
// adjust exam scores if necesssary
if (truefalse == "true")
{
bool = true;
exam1 = keyboard.nextInt();
exam2 = keyboard.nextInt();
exam3 = keyboard.nextInt();
}
else
{
average = ((exam1+exam2+exam3)/3);
}
为什么编译器会说“变量平均值可能尚未初始化”?