I have initialized and declared the variable "average." I do not understand why it won't compile. Does it have to do with the fact that it's in an if else? I've already tried "|| null" and that is not taking either. What to do?
// 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);
}
Why is the compiler saying "variable average might not have been initialized"?
truefalse == "true", you should be writingtruefalse.equals("true").double average = 0;