# Thread: Number guessing game bug?

1. Member
Join Date
Dec 2014
Location
St. Paul, Minnesota
Posts
4
Rep Power
0

## Number guessing game bug?

I have somewhat of an odd bug in a program that I wrote for one of my classes. The program is just a number guessing game, where the user must guess what number has been randomly generated between 0 and 100. They have five attempts to guess the number correctly. It's a very simple program and is working correctly, for the most part. There's just one thing I am scratching my head over. On the fifth attempt, even if the user guesses the correct number, the program will tell them that they have failed to guess the correct number in the given amount of tries they have. If you guess the correct number on any guess other than the fifth, the program will tell you that you have won the game. I really cannot figure out why this bug is happening. I have a feeling it's something incredibly simple that I'm overlooking.

I've just started learning Java in September, so sorry if I'm doing something not as efficiently as I could be.

Here is the code:

Java Code:
```package Pack1;

import javax.swing.*;

public class Class1 {

public static void main(String[] args) {

int num = (int)(Math.random()*100);
int guess;
int remainingGuess = 0;

String strGuess;

boolean done = false;

while(!done){

strGuess =
JOptionPane.showInputDialog("Enter a number between 0 & 100");
guess = Integer.parseInt(strGuess);
System.out.println();

int diff = Math.abs(num - guess);
remainingGuess++;                       //Adds one to int remainingGuess until it reaches 5, where the player will lose the game.
int remainingGuess1 = remainingGuess+1; //To output the correct current guess the user is on

if(remainingGuess==5){
JOptionPane.showMessageDialog(null, "You failed to guess the number. Good luck next time!");
System.exit(0);
}//End of if. When the user reaches 5 total guesses, the program closes.

if(diff == 0){
JOptionPane.showMessageDialog(null, "You guessed the correct number!");
done = true;
}//End of if

else
if(diff >= 50){
if(guess > num){
JOptionPane.showMessageDialog(null, "Your guess is extremely high. \n\nGuess again!" +
"\nAttempt "+remainingGuess1+"/5");
}//End of if
else
if(guess < num){
JOptionPane.showMessageDialog(null, "Your guess is extremely low. \n\nGuess again!" +
"\nAttempt "+remainingGuess1+"/5");
}//End of else if

}//End of else if

else
if(diff>=30 && diff<50){
if(guess > num){
JOptionPane.showMessageDialog(null, "Your guess is very high. \n\nGuess again!" +
"\nAttempt "+remainingGuess1+"/5");
}//End of if
else
if(guess < num){
JOptionPane.showMessageDialog(null, "Your guess is very low. \n\nGuess again!" +
"\nAttempt "+remainingGuess1+"/5");
}//End of else if

}//End of else if

else
if(diff>=15 && diff<30){
if(guess > num){
JOptionPane.showMessageDialog(null, "Your guess is high. \n\nGuess again!" +
"\nAttempt "+remainingGuess1+"/5");
}//End of if
else
if(guess < num){
JOptionPane.showMessageDialog(null, "Your guess is low. \n\nGuess again!" +
"\nAttempt "+remainingGuess1+"/5");
}//End of else if

}//End of else else if

else{
if(guess > num){
JOptionPane.showMessageDialog(null, "Your guess is a little high. \n\nGuess again!" +
"\nAttempt "+remainingGuess1+"/5"+num);
}//End of if
else
if(guess < num){
JOptionPane.showMessageDialog(null, "Your guess is a little low. \n\nGuess again!" +
"\nAttempt "+remainingGuess1+"/5"+num);
}//End of else if

}//End of else

}//End of while

}//End of main
}//End of class```
All help is appreciated, thanks!
Last edited by WWWIII; 12-13-2014 at 01:20 AM.

2. ## Re: Number guessing game bug?

First of all, the variable "remainingGuess" is really confusing. I'd expect it to start at 5, then countdown to 0. In your case it adds up, so it's more "pastGuesses" or something similar.

Besides that, you start your "remainingGuess" at 1, and after the user puts in his first number, it's 2, which is not correct. So after his fifth attempt, "remainingGuess" is 6 and that causes the program to exit.

I would start the remainingGuesses at 0 (and also rename it to something that reflects its function better)

3. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: Number guessing game bug?

Please post the code (in code tags), not an image.
I for one cannot read any of that.

4. Senior Member
Join Date
Jan 2014
Posts
137
Rep Power
0

## Re: Number guessing game bug?

I want to help but I cannot read the image...

5. ## Re: Number guessing game bug?

I agree an image is no optimal, but if you click the image twice, you will see it as a whole, Firefox lets you see the 100% version whichi s perfectly readable.

To WWWIII: Can you remove the image and replace it with the actual code in [code][/code] tags?

6. Member
Join Date
Dec 2014
Location
St. Paul, Minnesota
Posts
4
Rep Power
0

## Re: Number guessing game bug?

Sorry! It was a little bit hard to read.

7. ## Re: Number guessing game bug?

Edit: think about it: if I guess on my fifth attempt, what happens next? A message and System.exit(0); Nothing that says if I am correct or not.

8. Member
Join Date
Dec 2014
Location
St. Paul, Minnesota
Posts
4
Rep Power
0

## Re: Number guessing game bug?

Thanks, your suggestion does make perfect sense to me. I do believe I did exactly what you suggested, which is now reflected in the code (starting remainingGuess at 0 and counting up to 5). The problem still persists, however. I suppose I could try starting at five and count down to zero, to see if that makes a difference. I will do that in a bit and let you know the results.

Edit: I seeeee. Yeah, I suppose that would be a problem. I'll work on that in a little bit and let you know the outcome. I have a different program to finish before that, as it's due at midnight.
Last edited by WWWIII; 12-13-2014 at 02:04 AM.

9. Member
Join Date
Dec 2014
Location
St. Paul, Minnesota
Posts
4
Rep Power
0

## Re: Number guessing game bug?

Originally Posted by SurfMan
Edit: think about it: if I guess on my fifth attempt, what happens next? A message and System.exit(0); Nothing that says if I am correct or not.

It turns out that was the problem, and I was able to fix it. Though, I'm not sure if what I did was the best thing I could have done.

Java Code:
```if(remainingGuess>5){
if(diff==0){
done = true;
}//End of if
else{
JOptionPane.showMessageDialog(null, "You failed to guess the number. Good luck next time!");
System.exit(0);
}//End of else```
I remember why I initially set remainingGuess to 1 (I haven't thought of a better name for it yet, haha). If it's set on zero, you will be allowed to guess a total of six times, as zero counts as a try; 0 (1st try), 1 (2nd try), 2 (3rd try), 3 (4th try), 4 (5th try), 5 (6th try), which is not what I want.

Thank you so much though! The help was definitely appreciated and the program is now working as it should thanks to you :)
Last edited by WWWIII; 12-14-2014 at 11:37 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•