Return letter grade to main method
Main Method:
System.out.println("Student Name\tHigh Score\tLow Score\tAverage\t\tGrade");
for (int p = 0; p < Student.length; p++) {
System.out.println(Student[p] + "\t\t" + highGrade(studentGrades[p])
+ "\t\t" + lowGrade(studentGrades[p]) + "\t\t"
+ averageGrade(studentGrades[p]) + "\t\t" + letterGrade(p));
__________________________________________________ __________________________________________________ __________________
public static String letterGrade(double arrayIn[]) {
double avgGrade = 0.0;
for (int i = 0; i < arrayIn.length; i++) {
avgGrade += arrayIn[i];
double newavg = avgGrade / arrayIn.length;
if (newavg >= 94) {
return "A";
} else if (newavg >= 90) {
return "A-";
} else if (newavg >= 87) {
return "B+";
} else if (newavg >= 84) {
return "B";
} else if (newavg >= 80) {
return "B-";
} else {
return "F";
}
}
}
__________________________________________________ ______________________
It tells me I am missing me return statement. So Im not sure how to make my letter grades return from me if else statements. I am a beginner. Also thanks for your help!
Jason
Re: Return letter grade to main method
The warning is because the compiler does not see a guarantee that one of the returns will always succeed in the for loop. Assuming the loop completes without returning, the compiler wants to know what to return at that point. Usually this is the location to place your "default" value, or the "catch all". In this case "F" seems to be your "catch all". It has to be returned outside the loop.
Hoping you understand what is happening above, now it is time to say, A method can return only one value each time it is called. So the idea of returning inside a loop will end after the first return is found.
Edit:
please see announcements for the use of code tags and other information
Re: Return letter grade to main method
Thank you! Sometimes it just takes someone else to look at it in order to tip you off in the right direction. I appreciate it!
My Solution:
Main:
System.out.println("Student Name\tHigh Score\tLow Score\tAverage\t\tGrade");
for (int p = 0; p < Student.length; p++) {
System.out.println(Student[p] + "\t\t" + highGrade(studentGrades[p])
+ "\t\t" + lowGrade(studentGrades[p]) + "\t\t"
+ averageGrade(studentGrades[p]) + "\t\t" + letterGrade(studentGrades[p]));
}
__________________________________________________ ______________________
public static String letterGrade(double arrayIn[]) {
double avgGrade = 0.0;
for (int i = 0; i < arrayIn.length; i++) {
avgGrade += arrayIn[i];
double newavg = avgGrade / arrayIn.length;
if (newavg >= 94) {
return "A";
} else if (newavg >= 90) {
return "A-";
} else if (newavg >= 87) {
return "B+";
} else if (newavg >= 84) {
return "B";
} else if (newavg >= 80) {
return "B-";
} else if (newavg >= 77) {
return "C+";
} else if (newavg >= 74) {
return "C";
} else if (newavg >= 70) {
return "C-";
} else if (newavg >= 67) {
return "D+";
} else if (newavg >= 64) {
return "D";
} else if (newavg >= 60) {
return "D-";
}
else {
}
}
return "F";
}
Re: Return letter grade to main method
Did you read the part about returning inside a for loop?