I'm sorry if this isn't the right place to ask this question, so go easy on me.
I'm very novice with
Java, but I'm still trying to learn from a few books and online tutorials here and there.
Please keep in mind, it may not be very organized, so don't crucify me for that, but any constructive critics will be welcomed!
I'm having issues when compiling. I keep getting the error:
variable gpa might not have been initialized
System.out.println("Your GPA is " + gpa);
^
For the life of me, I cannot find out what I am doing wrong.
The point of this little program is to take an average of all of your class scores, and create a GPA for it.
Anything that you can help with me would be so much appreciated!
Here is the code:
//Computes the Average of a series of scores
import jpb.*;
public class avgScores{
public static void main(
String[]args){
//prompt user to enter a number of scores
SimpleIO.prompt("Enter the number of classes you want averaged: ");
String userInput = SimpleIO.readLine().trim();
int numberOfScores = Integer.parseInt(userInput);
System.out.println();
//create and array to hold scores
int [] scores = new int [numberOfScores];
//Prompt user to enter scores, then store in array
for (int i = 0; i < scores.length; i++) {
SimpleIO.prompt("Enter score for class " + (i + 1) + ": ");
userInput = SimpleIO.readLine().trim();
scores [i] = Integer.parseInt(userInput);
}
//Compute the Sum of Scores
int sum = 0;
int avgScore = sum / scores.length;
for (int i = 0; i < scores.length; i++)
sum += scores[i];
//Display Avg Score
System.out.println("\nAverage Score is " + sum / scores.length);
//Calculate GPA
double gpa;
if (avgScore <= 59) gpa = 0;
else if (avgScore <= 60) gpa = 0.7;
else if (avgScore <= 61) gpa = 0.8;
else if (avgScore <= 62) gpa = 0.8;
else if (avgScore <= 63) gpa = 0.9;
else if (avgScore <= 64) gpa = 1.0;
else if (avgScore <= 65) gpa = 1.0;
else if (avgScore <= 66) gpa = 1.1;
else if (avgScore <= 67) gpa = 1.2;
else if (avgScore <= 68) gpa = 1.3;
else if (avgScore <= 69) gpa = 1.4;
else if (avgScore <= 70) gpa = 1.5;
else if (avgScore <= 71) gpa = 1.6;
else if (avgScore <= 72) gpa = 1.7;
else if (avgScore <= 73) gpa = 1.8;
else if (avgScore <= 74) gpa = 1.9;
else if (avgScore <= 75) gpa = 2.0;
else if (avgScore <= 76) gpa = 2.1;
else if (avgScore <= 77) gpa = 2.2;
else if (avgScore <= 78) gpa = 2.3;
else if (avgScore <= 79) gpa = 2.4;
else if (avgScore <= 80) gpa = 2.5;
else if (avgScore <= 81) gpa = 2.6;
else if (avgScore <= 82) gpa = 2.7;
else if (avgScore <= 83) gpa = 2.8;
else if (avgScore <= 84) gpa = 2.9;
else if (avgScore <= 85) gpa = 3.0;
else if (avgScore <= 86) gpa = 3.1;
else if (avgScore <= 87) gpa = 3.2;
else if (avgScore <= 88) gpa = 3.3;
else if (avgScore <= 89) gpa = 3.4;
else if (avgScore <= 90) gpa = 3.5;
else if (avgScore <= 91) gpa = 3.5;
else if (avgScore <= 92) gpa = 3.6;
else if (avgScore <= 93) gpa = 3.7;
else if (avgScore <= 94) gpa = 3.8;
else if (avgScore <= 95) gpa = 3.9;
else if (avgScore <= 96) gpa = 3.9;
else if (avgScore <= 97) gpa = 4.0;
else if (avgScore <= 98) gpa = 4.0;
else if (avgScore <= 99) gpa = 4.0;
else if (avgScore <= 100) gpa = 4.0;
//Print GPA
System.out.println("Your GPA is " + gpa);
System.out.println("Thanks for using my program!");