I just started learning java a week ago, and I'm trying to make my first program - a simple grade calculator for a class I'm in - and I can't get it to compile for the life of me. I've finally gotten it down to one error ("Comp202Grader.java:60: variable letterGrade might not have been initialized") but I have no idea why it's saying that! It's easier if I just show you:
public class Comp202Grader {
public static void main(String[] args) {
java.util.Scanner reader = new java.util.Scanner(System.in);
System.out.println("Enter your name:");
String studentName = reader.nextLine();
System.out.println("Enter your 4 assignment grades (integers):");
int assign1 = reader.nextInt();
int assign2 = reader.nextInt();
int assign3 = reader.nextInt();
int assign4 = reader.nextInt();
double assignAvg = (assign1+assign2+assign3+assign4)/4;
System.out.println("Enter your midterm grade (decimal):");
double midtermExam = reader.nextDouble();
System.out.println("Enter your final exam grade (decimal):");
double finalExam = reader.nextDouble();
System.out.println(studentName + ", your summary is as follows:");
System.out.println("Average assignment grade: " + assignAvg);
double gradeWithMidterm = (assignAvg/100)*20 + (midtermExam/100)*20 + (finalExam/100)*60;
System.out.println("Final grade (counting midterm): " + gradeWithMidterm);
double gradeNoMidterm = (assignAvg/100)*20 + (finalExam/100)*80;
System.out.println("Final grade (without midterm): " + gradeNoMidterm);
double gradeNumber;
if(gradeWithMidterm>gradeNoMidterm) {
gradeNumber = gradeWithMidterm;
} else {
gradeNumber = gradeNoMidterm;
}
System.out.println("Your final percent score: " + gradeNumber);
System.out.println("Your final letter grade: " + getLetterGrade(gradeNumber));
}
public static String getLetterGrade(double gradeNumber) {
String letterGrade;
if(gradeNumber>=85) {
letterGrade = "A";
}
else if((85>gradeNumber)&&(gradeNumber>=80)) {
letterGrade = "A-";
}
else if((80>gradeNumber)&&(gradeNumber>=75)) {
letterGrade = "B+";
}
else if((75>gradeNumber)&&(gradeNumber>=70)) {
letterGrade = "B";
}
else if((70>gradeNumber)&&(gradeNumber>=65)) {
letterGrade = "B-";
}
else if((65>gradeNumber)&&(gradeNumber>=60)) {
letterGrade = "C+";
}
else if((60>gradeNumber)&&(gradeNumber>=55)) {
letterGrade = "C";
}
else if((55>gradeNumber)&&(gradeNumber>=50)) {
letterGrade = "C-";
}
else if(gradeNumber<50) {
letterGrade = "F";
}
return letterGrade;
}
}
Any help would be greatly appreciated! I really hope to become an active member of this forum with time...