File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 3168
On a stylistic point is usually written as

Graham Robinson

Joined: Nov 23, 2005
Posts: 16
ahh, thanks for the tip.
It is sorta covered in the JavaRanch Style Guide.
subject: Storing count within a while
Similar Threads
Do whiles/Fors and Drawing
Test score
Drawing in a loop
double or float?
finding an average