Results 1 to 7 of 7
- 01-14-2012, 02:39 AM #1
Feel like a genius. Answer my pathetic looping question.
Hey everyone. New to the forum. I want to get good at Java but it is painstaking so far.
Here is my question.
This is a tutorial for a command line application. I try to do all my exercises in Netbeans because I like GUIs.
Could someone be kind enough to tell me why this loops endlessly, and possibly how to fix it?
Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // Generate a random number to be guessed int number = (int)(Math.random() * 101); // Get the integer from the textbox int guess = Integer.parseInt(userInput.getText()); while (guess != number) { if (guess == number) answerOutput.setText("Yes, the number is " + number); else if (guess > number) answerOutput.setText("Your guess is too high"); else answerOutput.setText("Your guess is too low"); // End of loop }Last edited by Fubarable; 01-14-2012 at 02:46 AM. Reason: code tags added
-
Re: Feel like a genius. Answer my pathetic looping question.
Where do you change the value held by the guess variable inside of the loop?
Oh, and welcome to the java-forums!
In a minute, I'll add [code] [/code] tags to your code,... hang on!
- 01-14-2012, 03:05 AM #3
Re: Feel like a genius. Answer my pathetic looping question.
I read you sentence about 10 times, and I am confused.
I didn't think the value of guess would need to change...
Perhaps I should come back after I do more research.
I appreciate you helping me with this though.
-
Re: Feel like a genius. Answer my pathetic looping question.
Let's simplify. Say you have this bit of code:
Since x never changes inside of the loop, it has no chance of changing to a non-zero value, and the loop will never end. Contrast that with this loop:Java Code:int x = 0; while (x == 0) { System.out.println("x is 0 still"); }
Now x changes inside of the loop to 3, and the loop can exit. So the sentinal variable (in this case, x) must change inside of the loop for the code's behavior to change (in this case, in order to exit the loop).Java Code:int x = 0; while (x == 0) { System.out.println("x is 0 still"); x = 3; }
Clear as mud?
- 01-14-2012, 03:15 AM #5
Re: Feel like a genius. Answer my pathetic looping question.
Sweet. I got it.
Thanks for your help good sir.
I hate to be a bother with such trivial questions.
Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // Generate a random number to be guessed int number = (int)(Math.random() * 101); // Get the integer from the textbox int guess = -1; while (guess != number) { guess = Integer.parseInt(userInput.getText()); if (guess == number) answerOutput.setText("Yes, the number is " + number); else if (guess > number) answerOutput.setText("Your guess is too high"); else answerOutput.setText("Your guess is too low"); // End of loop }
-
Re: Feel like a genius. Answer my pathetic looping question.
- 01-14-2012, 03:21 AM #7
Similar Threads
-
Simple question! Please answer!
By Asvin in forum New To JavaReplies: 4Last Post: 11-09-2011, 01:53 AM -
A Rather Pathetic Question
By anthropamorphic in forum New To JavaReplies: 11Last Post: 07-06-2011, 07:57 PM -
please answer some basic question if you can
By togaurav in forum New To JavaReplies: 17Last Post: 04-16-2011, 08:23 AM -
A QUESTION U CAN ALL ANSWER, EXCEPT ME, (if not, come and sit with me lol)
By mansoor2 in forum New To JavaReplies: 12Last Post: 07-27-2010, 09:40 AM -
Plz answer this question ...
By raghu2114 in forum Advanced JavaReplies: 2Last Post: 09-19-2008, 06:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks