aspose file tools*
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
Author

Storing count within a while

Graham Robinson
Greenhorn

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


//for control
for(int count = 0; count < 12; count++)
{
do
{
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.

thanks
Graham Robinson
Greenhorn

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



thanks.
Joanne Neal
Rancher

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


Joanne
Graham Robinson
Greenhorn

Joined: Nov 23, 2005
Posts: 16
ahh, thanks for the tip.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Storing count within a while