Reputation: 13
I'm trying to write the classic beginner code where I make a random number, then ask the user to try to guess it. I tried to get a little fancy by also using JOptionPane. Everything works fine except the variable which counts the number of times the user has guessed the random number. It's always just one value less than what it should be (If I get it right on the third try, it says I got it right in two). How do I fix this?
Second question - I have out.println(randomNum); before I ask the user to guess the number (so I can cheat), but it doesn't print in the console until after I've guessed once. What's up with that? Any help would be greatly appreciated!
int randomNum = new Random().nextInt(11); //define random number
int guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
int numGuesses = 1;
out.println(randomNum); //cheater
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
while (guess != randomNum) {
++numGuesses; //to increase the value of numGuesses by 1 each time the while loop iterates
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
}
JOptionPane.showMessageDialog(null, "You got it right after only " + numGuesses + " tries!");
out.println(numGuesses); //To see if it matches the JOptionPane value
Upvotes: 0
Views: 98
Reputation: 31
1) You are asking the user to guess the number twice before you enter the while loop. Since numGuesses
is set to 1 and added by 1 on entering the while loop, you'll be off by one (you're asking the user to guess the number for the third time in the while loop).
2) Since you are printing your 'cheater'-code after you are prompting the user to guess the number it will only appear after that guess.
Please note that Random().nextInt(11)
includes 0 as well, so you might want to initialize guess
to -1 as in the example below:
int randomNum = new Random().nextInt(11); //Define random number between 0 (inclusive) and 10 (inclusive)
int guess = -1;
int numGuesses = 0;
out.println(randomNum); //Cheater
while (guess != randomNum) {
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 0-10: "));
numGuesses++; //To increase the value of numGuesses by 1 each time the while loop iterates
}
JOptionPane.showMessageDialog(null, "You got it right after only " + numGuesses + " tries!");
out.println(numGuesses); //To see if it matches the JOptionPane value
Upvotes: 0
Reputation: 2005
You're really close! What's going on is that you're taking two guesses before you enter the while
loop, and not incrementing the numGuesses
. Programmers often try to avoid duplicating code. Try this:
int randomNum = new Random().nextInt(11); //define random number
int guess = -1; // set it to an invalid value
int numGuesses = 0; // they haven't guessed at all at this point
out.println(randomNum); //cheater
while (guess != randomNum) {
++numGuesses; //to increase the value of numGuesses by 1 each time the while loop iterates
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
}
JOptionPane.showMessageDialog(null, "You got it right after only " + numGuesses + " tries!");
out.println(numGuesses); //To see if it matches the JOptionPane value
Upvotes: 2
Reputation: 347234
Don't ask the user twice for the random value
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
while (guess != randomNum) {
//...
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
Instead, use a do-while
loop, as you want them to make a guess at least once...
int numGuesses = 0;
do {
numGuesses++;
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
} while (guess != random);
Upvotes: 1
Reputation: 178263
You are having the user guess twice before even checking the guess, before you even enter the while
loop. Remove the second guess before the while
loop:
int guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
int numGuesses = 1;
System.out.println(randomNum); //cheater
// Remove the following line!
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
Upvotes: 1