This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Storing count within a while Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Storing count within a while" Watch "Storing count within a while" New topic

Storing count within a while

Graham Robinson

Joined: Nov 23, 2005
Posts: 16
int rain, heaviest = 0, entry;
boolean valid;
float sum, average;

//for control
for(int count = 0; count < 12; count++)
c.out.println("Please input value of rainfull betweem 0 and 200mm.");
rain = c.input.readInt();
valid = (rain >= 0 && rain <= 200);
if (!valid)
c.out.println("The value entered isn't valid.");
}while (!valid);

if (rain > heaviest)
heaviest = rain;

This should take 12 values from the user, but how would i create the sum of all entries, as I'm not storing them? Say if the user enters 5 and 6, i need the it to count the total, so i can work out the average. I've managed to get it to print the heaviest value, but im stuck with the average.

Graham Robinson

Joined: Nov 23, 2005
Posts: 16
It's ok, I've solved it now. Just incase anyone want's to know how i did it...

i used a float called total = 0.0f
then in the while i used, total = rain + total;
and an int entry,
as entry++

total / entry = average

Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
On a stylistic point is usually written as

Graham Robinson

Joined: Nov 23, 2005
Posts: 16
ahh, thanks for the tip.
I agree. Here's the link:
subject: Storing count within a while
It's not a secret anymore!