This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Loop issue Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Loop issue" Watch "Loop issue" New topic
Author

Loop issue

apollo abel
Ranch Hand

Joined: Mar 31, 2006
Posts: 32
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

Joined: Feb 24, 2004
Posts: 236
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 ]

Paul M. Santa Maria, SCJP
apollo abel
Ranch Hand

Joined: Mar 31, 2006
Posts: 32
Ok, the code now its done - well not really, is not an efficient code -
here it is:

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Loop issue
 
Similar Threads
calling a method w/out an object
Help me solve the "ANSWER" problem
Specific number
Math.random().....any formula to adjust required range?
catch statements