This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

High Low Average Problem

 
Jem Edwards
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to have the program compute the high score, low score and average of all scores. It's doing the high & low ok but it's giving me only something close to an average. I can't even figure out how the average is being computed to the number it spits out. Any help is greatly appreciated!
[ edited to remove tabs and fix indention -ds ]
[ April 03, 2003: Message edited by: Dirk Schreckmann ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how SavitchIn works, but aren't you reading two entries in (the highlighted lines) and not adding the first score to either the total or the number of games played?
int max=SavitchIn.readLineInt();
int min=max; //The max and min so far are the first score
int next = SavitchIn.readLineInt();
int numberOfGames = 0; //number of bowling games
int total = 0; //sum of bowling game scores
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easyest way of figuring out what your program is doing is to step though each line of code using a debugger. If you don't have a debugger put a lot of print statements. Each time you add another number to the total print it out.
"Adding number X to the running total of Y, the current count is Z."
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are omitting the first score from the average and the number of games. Test this by entering only one score. Given your code, you will get a high and low, but a Divide by Zero exception when it is calculating the average.
Try this instead:

This should calculate the numbers correctly.
 
Jem Edwards
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. Our internet class discussion group was figuring that there was something about the numbers the program was reading in but we couldn't figure it out.
Thanks a bunch!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic