| Author |
Please Help!
|
Ryon Groff
Greenhorn
Joined: Nov 14, 2010
Posts: 4
|
|
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!");
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
What happens if the avgscore is greater than 100? Will the gpa variable be set in that case?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
Local variables are not automatically initialized. "double gpa;" is not initialized, and there is a chance that none of the conditions (the huge if/else) will be met. We usually initialize local variables when we declare them.
|
[My Blog]
All roads lead to JavaRanch
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
And please UseAMeaningfulSubjectLine next time.
|
 |
Bharath Raja
Ranch Hand
Joined: Jan 21, 2009
Posts: 111
|
|
Ryon Groff wrote:I'm sorry if this isn't the right place to ask this question, so go easy on me.
this is the right place
Ryon Groff wrote:
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!
dont hesistate to raise questions here.. we are all here for learning purpose only.
Ryon Groff wrote:
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);
^
try to study the variables... local variable must be initialized with some values... hope the follow will clear your problem
double gpa=0.0;
cheers
|
Life is either daring something or nothing - Helen Keller
|
 |
Ryon Groff
Greenhorn
Joined: Nov 14, 2010
Posts: 4
|
|
I guess I'm just not ready to tackle a program like this.
When I try to initialize the gpa double, ex. double gpa = 1
The final output of the program doesn't change the gpa at all, it stays = to 1.
And sorry about the subject line, i'll do better next time!
|
 |
Ryon Groff
Greenhorn
Joined: Nov 14, 2010
Posts: 4
|
|
Thank you very much!
Solved my problem!
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
And welcome to JavaRanch
|
 |
 |
|
|
subject: Please Help!
|
|
|