Win a copy of Spring Boot in Practice this week in the Spring forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Liutauras Vilda
• Henry Wong
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Mikalai Zaikin
• Himai Minh

Don't understand why one way works but not the other

Ranch Hand
Posts: 42
1
• Number of slices to send:
Optional 'thank-you' note:
I'm taking an intro to Java course (some may have seen previous questions posted).

The code below is for an interactive example from my text. I didn't write the majority of the code, the scenario is:

Modify the program to print the average (mean) as well as the sum. Hint: You don't actually have to change the loop, but rather change what you print.

This is the initial modification that I attempted. It did not provide the proper output.
I added line 21, as well as line 26

Output for the above code was:

Enter 8 integer values...
Value:
Value:
Value:
Value:
Value:
Value:
Value:
Value:
Sum: 920
Average: 0

based on input of:
3 5 234 346 234 73 26 -1.

If I remove the variable initialization at line 21, and then I change line 26 to do the same thing that I was trying to do at line 21,
everything works out fine. I achieve proper output with the Average computed properly at 115.

Code that worked:

What I don't understand is why the second way worked, but the first didn't. Was I not essentially doing the same thing, only with a variable?

Thanks

Bartender
Posts: 732
10
• 1
• Number of slices to send:
Optional 'thank-you' note:
In your first example, you calculate avgVal on line 21 BEFORE you calculate the sumVal. That is, sumVal is still zero.
In the second example, you calculate the average AFTER you have summed up all of the values.

Marshal
Posts: 3895
542
• Number of slices to send:
Optional 'thank-you' note:

Bill Platt wrote:This is the initial modification that I attempted. It did not provide the proper output.
I added line 21, as well as line 26

It looks like you are calculating the average before calculating the sum of all the entered values (which is used in your average calculation).

Try calculating your average after completing the summing work.

Bill Platt
Ranch Hand
Posts: 42
1
• Number of slices to send:
Optional 'thank-you' note:

Fred Kleinschmidt wrote:In your first example, you calculate avgVal on line 21 BEFORE you calculate the sumVal. That is, sumVal is still zero.
In the second example, you calculate the average AFTER you have summed up all of the values.

OK, thanks. I see that now.

One of these days I will get where I am thinking in the proper circles.

It didn't even occur to me that the second loop was doing the calculations for sumVal.

That probably should have been obvious.

Bill

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?