Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Storing count within a while

 
Graham Robinson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a stylistic point is usually written as
 
Graham Robinson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh, thanks for the tip.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic