Log in

View Full Version : Guessing game with loops


Jess
July 17th, 2013, 07:11 PM
I'm nearly done completing this assignment; just need to sort out an error

/* Jessica Chen
* Wednesday 6/17/13
* Java 1.7.0_21 and NetBeans 7.3
* This program will allow a user to guess the computer number 1 to 100
*/
import javax.swing.JOptionPane;

public class GuessingGameLoop {
public static void main(String[] args) {
int computerNumber = (int) (Math.random() * 100 + 1);
System.out.println("The correct guess would be " + computerNumber);
for (int count = 1; userAnswer != computerNumber; count++){
String response = JOptionPane.showInputDialog(null, "Enter a guess "
+ "between 1 and 100","Guessing Game", 3);
int userAnswer = Integer.parseInt(response);
JOptionPane.showMessageDialog(null, "Your guess is "
+ determineGuess(userAnswer, computerNumber) + "\nGuesses: "
+ count);
}
}

public static String determineGuess (int userAnswer, int computerNumber){

if (userAnswer <= 0 || userAnswer >= 100) {
// if the number is below 0 or above 100, it is invalid
return "invalid";
}
else if (userAnswer == computerNumber) {
return "correct";
}
else if (userAnswer > computerNumber) {
return "too high";
}
else if (userAnswer < computerNumber) {
return "too low";
}
else {
return "correct";
}
}

}


I get an error on userAnswer in "int count = 1; userAnswer != computerNumber", saying it cannot find the symbol. I need the looping to end when the user guesses the correct number. What needs to be fixed?

ethanf93
July 18th, 2013, 12:02 AM
// code ...
for (int count = 1; userAnswer != computerNumber; count++){
// code ...
int userAnswer = Integer.parseInt(response);
// code ...
}

Consider: What is the value of userAnswer the first time you go through the loop?

You cannot use a variable before you define it.

Jess
July 19th, 2013, 10:06 PM
I'm still stuck :/

TheMatrix
July 20th, 2013, 01:18 AM
Have you tried the debugger? I've never used the Java debugger(I seldom use Java), but you should be able to show the contents of variables, like when they're in scope, what their value is, and so forth.
I am certain that there are many tutorials on the internet for this, just look it up. If you use an IDE such as Eclipse or NetBeans, it is also possible their website has a tutorial on how to use the IDE integration for the debugger.

Then you have the fallback debugger: Rubber Ducking (https://en.wikipedia.org/wiki/Rubber_duck_debugging). Explain every line of code in as much detail as possible. If it doesn't make sense to you, chances are it doesn't make sense to the duck, and likewise to the computer.

ethanf93
July 20th, 2013, 09:16 AM
Ok, so a debugger is not going to help you with a compiler error which is what you're seeing.

The problem here is that the compiler doesn't know what userAnswer means. As I'm sure you know, you need to tell the compiler pretty explicitly about variables: what they're called and what they hold.

Take a look through the program above the line that's showing the error: did you tell the computer what userAnswer is?

Jess
July 20th, 2013, 07:29 PM
Well, the answer to the question is no, so I know I have to give userAnswer a value...but I don't know what. I've tried several ways. I've removed int userAnswer = Integer.parseInt(response); then set userAnswer equal to zero, placing it out of the for loop; that didn't work. I've tried removing the initial value and just making it int userAnswer; ......nope.

TheMatrix
July 20th, 2013, 09:09 PM
Since you can't figure it out, I'll give you a hint: it's in the main function.

Specifically, this will not work:

for (int count = 1; userAnswer != computerNumber; count++){

since you have not defined userAnswer yet. You do initialise it later, but by then it is too late: neither the compiler nor the runtime will have found it where you used it.
You are comparing a non-existant value to a number, basically.

To make it work, put something like this:

int userAnswer = 0; //put it here with a value that won't satisfy for's second argument before you start the loop
for (int count = 1; userAnswer != computerNumber; count++){
//...
userAnswer = Integer.parseInt(response); //you have already declared userAnswer by now, and putting "int" before it is an error
//...


HTH

Jess
July 21st, 2013, 01:23 PM
I decided to change it a bit, so that it uses a while loop. after some adjustment and adding I got it to work as it should. Thanks anyways, Thomas. Also, thanks Ethan. If you want to know what my final code is, I'll gladly post it.