• Post Reply Bookmark Topic Watch Topic
  • New Topic

clues on how to exit this while loop, please?  RSS feed

 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got the proverbial high-low guessing game. The point of the exercise is to practice throwing exceptions.

The program compiles and runs fine - but I cannot seem to troubleshoot why it won't quit. When you guess the correct number, it keeps asking for another integer. It appears that it cannot exit the while loop. I tried using a boolean stopflag = false, and while (!stopflag), and while (stopflag = false)... with the correct returning a boolean true. That did not work. So, I tried using an int flag, but that isn't working either. I'm guessing it must be something in my logic that isn't working. Could one of you kind folks help me get pointed in the right direction?

Thank you!




 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you are expecting stopflag to be set in your test() method and returned to stop your while loop. This doesn't work because ints are passed by value when passed as arguments on methods. So when you call test.test(guess, answer, stopflag) a copy of stopflag is passed to the test() method. The test() method modifies the copy of the stopflag. But the copy doesn't get copied back to the original stopflag variable.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After looking at your code closer, I see I misunderstood your intentions. You are not setting stopflag in the test() method. You are returning stopflag==1 but that will always return false because stopflag is 0. But then you never check the return value anyway.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!