Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Min, Max, and Average

 
Angela Hill
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happy to help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic