Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Help with Min, Max, and Average

Angela Hill
Greenhorn
Posts: 19
Hello All,
I would love if anybody could please help me with coding the math part of Min Max and Avg. I am having a difficult time trying to call to my variables to get the correct answer.
here is my code so far...

Also, I cannot get my loop to stop for my avg. it says infinity. lol

Daryl Cofer
Greenhorn
Posts: 19
Where do you increment your count variable?

Edit: Nevermind I see it. Try moving your count++ outside of your assignment for Avg.

Count++
Avg = Num+Num/Count

Edit: Also take into consideration order of operations. You want to add the two numbers before you divide by the count so:

Count++
Avg = (Num+Num)/Count

Angela Hill
Greenhorn
Posts: 19
this is my updated code. However when I put in an input say 1 2 3. my output is:Please enter your numbers.
1 2 3
The Average value is: 1.0
Maximum: 1
Minimum: 0
which is obviously not correct.

Daryl Cofer
Greenhorn
Posts: 19

That should do it. All I did was add a while loop that checks to see if there are any more tokens to look through.

Angela Hill
Greenhorn
Posts: 19
Thank you so much!!!
One last thing when I do the input the min comes out as 0 every time any suggestions to prevent this?

Daryl Cofer
Greenhorn
Posts: 19
Change the values when you initialize your min and max values.

Integer.MAX_VALUE and MIN_VALUE are built-in Java constants. They are the largest and smallest values that can be represented in integer form. So by setting the Max to Integer.MIN_VALUE, any number you enter is automatically going to re-assign the Max variable. Same goes with the Min variable.

Angela Hill
Greenhorn
Posts: 19
Ah you are a saint I would have never of thought of that...then again i'm pretty new at all this lol Thanks again

Daryl Cofer
Greenhorn
Posts: 19
Happy to help!