Results 1 to 9 of 9
  1. #1
    WWWIII is offline Member
    Join Date
    Dec 2014
    Location
    St. Paul, Minnesota
    Posts
    4
    Rep Power
    0

    Default 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!
    Attached Thumbnails Attached Thumbnails Number guessing game bug?-codesample.jpg  
    Last edited by WWWIII; 12-13-2014 at 01:20 AM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default 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)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Number guessing game bug?

    Please post the code (in code tags), not an image.
    I for one cannot read any of that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default Re: Number guessing game bug?

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

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default 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?
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

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

    Default Re: Number guessing game bug?

    Sorry! It was a little bit hard to read.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Number guessing game bug?

    Did you read my reply at #2?

    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's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

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

    Default 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. #9
    WWWIII is offline Member
    Join Date
    Dec 2014
    Location
    St. Paul, Minnesota
    Posts
    4
    Rep Power
    0

    Default Re: Number guessing game bug?

    Quote Originally Posted by SurfMan View Post
    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.

    All I did was this:

    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.

Similar Threads

  1. Reverse Number Guessing Game--in 10 tries???
    By ctkim in forum New To Java
    Replies: 2
    Last Post: 11-02-2013, 04:08 AM
  2. Help With Random Number Guessing Game
    By carlyspade in forum New To Java
    Replies: 15
    Last Post: 07-21-2013, 10:57 PM
  3. Number Guessing Game issue
    By gabrielpr12 in forum New To Java
    Replies: 14
    Last Post: 01-25-2012, 12:06 AM
  4. Reverse Number guessing game
    By rarman555 in forum New To Java
    Replies: 6
    Last Post: 04-24-2011, 02:46 AM
  5. Java - number guessing game
    By kev670 in forum Java Applets
    Replies: 3
    Last Post: 10-22-2010, 01:55 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
  •