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

Loop issue

 
apollo abel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, i've been trying since like 2 days ago to solve this problem, but i havent been able to do so.
this is the error i get:

As you can see the game ends when i enter a number higher than the number of marbles. however, i dont want the game to end, i want it ask the question again, until the user enters a number thats is not greater than the number of marbles, and that is not greater than the number of marbles divided by 2.
this is the code i have so far, i've tried making a boolean method but it always escape the program, i want it to repeat the question:

this is the code i have so far:


thanks for the support
[ April 01, 2006: Message edited by: apollo abel ]
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To prevent the exception, you need something like this:

But the real problem is that you probably want to constrain numMarbles to some number between min and the number of marbles actually left. For example:

As far as exiting the loop - I confess I haven't looked carefully at your code. It can definitely be simplified ... but as far as I can tell, once you compute the random value and pcMarblesTaken correctly, then it looks like numMarbles should eventually go down to zero and the loop exit. So you should be OK...

'Hope that helps .. PSM
[ April 01, 2006: Message edited by: Paul Santa Maria ]
 
apollo abel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the code now its done - well not really, is not an efficient code -
here it is:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic