Random number is 27
guess #1 = 50 ... next guess is 1-50
guess #2 = 25 ... next guess is 25-50
guess #3 = 37 ... next guess is 25-37
and so on... but I get caught in an infinite loop and I am not sure why... any suggestions?
Yup, I got that far, the loop is there to keep the count of the number of guesses and the high and low. I think I need to add the checkGuess inside the while loop but I am not sure who I could do that, and if I recall the function again I will reinitialize all the variables.
Bear Bibeault wrote:Your click handler checks the answer by going into a loop. If the answer is wrong, how does the user get a chance to enter another guess?
Hint: ponder on why the loop is there in the first place.
Bear Bibeault wrote:OK, I'll ask the question one more time, so please think about it a bit:
Herein lies the danger in choosing an approach before even knowing the problem. This isn't a matter of "I know there are other ways" -- it's a matter that a loop cannot be used for this. That's not the way GUIs work.
In a Java console program, you can loop and stall waiting for input from the keyboard. That is not the case with GUIs such as web pages, or even Java GUIs such as Swing or JavaFX.
Such program are event-driven; you don't stall waiting for events, you register handlers that get called when events occur.
The reason that you have an infinite loop is that in response to a single event, you put the code into loop that's expecting to handle multiple events, when it can only handle a single event.
A click event handler handles one click, and one click only.
So, if your aim is to learn event handling, your code is way off the mark. If the goal is to learn loops, user event handling is not where that is going to happen. Choose something like converting an array of Fareheit values to Celcisus or some other data collection manipulation task.