problem with initializing
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
New to the forum - go easy on me
I am dealing with aggregates and i have created a program that will ask for user input to determine a student average. I ask the user to enter test score 1,2 and 3 for student number one, and then ask teh same info for student number 2. the problem is that i call teh variable testScore1,testScore2,testScore3. As a result, student 2 will output the same grade values as student one. I believe teh problem is that Java has no way to know that once student one's average is calculated taht it must set the counter back to zero and now use new values to calculate the average for student 2. I'm not sure how to do this wehn i've used a constructor to initialize the testScores to zero.
Here is my code.
I am dealing with aggregates and i have created a program that will ask for user input to determine a student average. I ask the user to enter test score 1,2 and 3 for student number one, and then ask teh same info for student number 2. the problem is that i call teh variable testScore1,testScore2,testScore3. As a result, student 2 will output the same grade values as student one. I believe teh problem is that Java has no way to know that once student one's average is calculated taht it must set the counter back to zero and now use new values to calculate the average for student 2. I'm not sure how to do this wehn i've used a constructor to initialize the testScores to zero.
Here is my code.
Being a scholar of life does not require a student loan.
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Aside from some minor formatting and style issues, your code looks correct.
What's happening when you execute it and put in different values? Your testScore variables are independent for each student object that you create (inside the student object), so there shouldn't be any problem with that. As far as the average, there is no "counter" it just returns a computation based on the variables that are, again, independent from other students within your student object. I don't see any showstoppers on first pass... ???
What's happening when you execute it and put in different values? Your testScore variables are independent for each student object that you create (inside the student object), so there shouldn't be any problem with that. As far as the average, there is no "counter" it just returns a computation based on the variables that are, again, independent from other students within your student object. I don't see any showstoppers on first pass... ???
Vee Shall
Greenhorn
Posts: 7
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
i don't have any opportunity to enter a value for the second student (marsha) because i get compile errors. The program is not recognizing the difference between
John's testScore1 and Marsha's testScore1. When i try to compile it's saying that its already defined most likely because we already referenced these score values when we got John's
average.
StudentBody.java:54: testScore1 is already defined in main(java.lang.String[])
int testScore1 = scan.nextInt();
^
StudentBody.java:56: testScore2 is already defined in main(java.lang.String[])
int testScore2 = scan.nextInt();
^
StudentBody.java:58: testScore3 is already defined in main(java.lang.String[])
int testScore3 = scan.nextInt();
^
these compile errors are happening at the point where i request input for the second students scores
John's testScore1 and Marsha's testScore1. When i try to compile it's saying that its already defined most likely because we already referenced these score values when we got John's
average.
StudentBody.java:54: testScore1 is already defined in main(java.lang.String[])
int testScore1 = scan.nextInt();
^
StudentBody.java:56: testScore2 is already defined in main(java.lang.String[])
int testScore2 = scan.nextInt();
^
StudentBody.java:58: testScore3 is already defined in main(java.lang.String[])
int testScore3 = scan.nextInt();
^
these compile errors are happening at the point where i request input for the second students scores
Being a scholar of life does not require a student loan.
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You can only have uniquely named variables. However, you can reassign values to a variables once its declared.
Just switch
to
Similarly for testScore2 and testScore3.
Just switch
to
Similarly for testScore2 and testScore3.
Bert Wilkinson
Ranch Hand
Posts: 33
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
oops. Missed that!
Got carried away with the claims of bad output when the code doesn't even compile....harrumph.
Got carried away with the claims of bad output when the code doesn't even compile....harrumph.
Vee Shall
Greenhorn
Posts: 7
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hoo haa!! that worked.......
but just so i'm clear on how this is working............if i added on to this program to allow averages for 5 students isntead of 2 , would this line of code " testScore1 = scan.nextInt(); "
allow me to pass a new value to the test score?
but just so i'm clear on how this is working............if i added on to this program to allow averages for 5 students isntead of 2 , would this line of code " testScore1 = scan.nextInt(); "
allow me to pass a new value to the test score?
Being a scholar of life does not require a student loan.
| My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











