Web Design and Web Development Forum

  1. #1
    Stealther's Avatar
    Join Date
    Sep 2009
    Location
    In the code in the US
    Age
    16
    Posts
    82
    Rep Power
    3
  2. Stealther is on a distinguished road
  3. Help with Guessing Game

    Hey there....I have a simple problem...well I think it might be a logical error but I don't know...I made a simple number guessing game in Java the other day but I want to improve it a little more...I want the person to be able to play the game again without having to compile the and run the program again!

    Anyway...here is my simple code:
    Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Guessing {
    
    	public static void main(String[] args) 
    	{
    		final int MAX = 10;
    		int guess, answer;
    		String another = "y";
    		Scanner scan = new Scanner(System.in);
    		
    		
    		Random generator = new Random();
    		answer = generator.nextInt(MAX) + 1;
    		
    		System.out.print
    			(
    				"I'm thinking of a number between 1 and " + MAX +
    				". Guess what it is: "
    			);
    		guess = scan.nextInt();
    		
    		while(another.equalsIgnoreCase("y"))
    		{
    
    			if (guess == answer){
    		
    				System.out.println
    					(
    					"You got it correct! Good guessing!"
    					);
    		
    			}else{
    		
    			System.out.println
    					(
    						"Sorry, but that is incorrect."
    					);
    				System.out.println
    					(
    						"The correct number was " + answer +
    						"."
    					);
    			}
    		
    			System.out.println();
    			System.out.print
    				(
    					"Play Again?(Y/N)"
    				);
    			another = scan.nextLine();
    		}
    	}
    }
    See...the code prints out Play Again?(Y/N) but it doesn't allow input for some reason...!
    Reply With Quote Reply With Quote
  4. #2
    Stealther's Avatar
    Join Date
    Sep 2009
    Location
    In the code in the US
    Age
    16
    Posts
    82
    Rep Power
    3
  5. Stealther is on a distinguished road
  6. Re: Help with Guessing Game

    Oh hey sorry for double posting but I left out some info...! All the semicolons are there...the braces are all balanced and so are the parentheses so this can't be a syntax error.
    Reply With Quote Reply With Quote
  7. #3
    Join Date
    Apr 2006
    Location
    Texas, USA
    Age
    19
    Posts
    1,643
    Rep Power
    8
  8. ddreier is on a distinguished road
  9. Re: Help with Guessing Game

    You probably need to move the 'game part' of the program into a separate function or method (or whatever Java calls it). Then you can use a do-while loop (or plain while loop) to check the input and re-start the game part as per the user's response.

    Pseudo Code:
    Code:
    string input = <however you get the input>;
    
    while(input == "y")
    {
        playGame();
    }

    Cool new programming forum: Sigcont.com

    If you're going to post code, please use [code][/code] tags!
    ddreier.com
    WooHoo!
    Reply With Quote Reply With Quote
  10. #4
    Stealther's Avatar
    Join Date
    Sep 2009
    Location
    In the code in the US
    Age
    16
    Posts
    82
    Rep Power
    3
  11. Stealther is on a distinguished road
  12. Re: Help with Guessing Game

    For some reason that doesn't work. Anyway I will set that aside for some time now. What I do want is to make a while loop so if an invalid input is put (such as -222) the game will output "Please enter a valid number: ". See I have the while loop but it doesn't work for some reason.
    Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Guessing 
    {
    	public static void main(String[] args) 
    	{
    		final int MAX = 10;
    		int guess, answer;
    		String another = "y";
    		Scanner scan = new Scanner(System.in);
    		
    		
    		Random generator = new Random();
    		answer = generator.nextInt(MAX) + 1;
    		
    		System.out.print
    			(
    				"I'm thinking of a number between 1 and " + MAX +
    				". Guess what it is: "
    			);
    		guess = scan.nextInt();
    		
    		while(answer < 1 || answer > MAX)
    		{
    			System.out.print
    				(
    					"Please enter a valid number: "
    				);
                            answer = scan.nextInt();
    		}
    		
    		if (guess == answer)
    		{
    			System.out.println
    				(
    				"You got it correct! Good guessing!"
    				);
    		
    		}else{
    		
    		System.out.println
    				(
    					"Sorry, but that is incorrect."
    				);
    			System.out.println
    				(
    					"The correct number was " + answer +
    					"."
    				);
    		}
    	}
    }
    Reply With Quote Reply With Quote
  13. #5
    callumjones's Avatar
    Join Date
    Mar 2005
    Location
    Perth, Australia
    Age
    21
    Posts
    3,335
    Rep Power
    12
  14. callumjones has a spectacular aura about callumjones has a spectacular aura about
  15. Re: Help with Guessing Game

    I think you are mixing up your answer and guess variables
    Code:
    while(answer < 1 || answer > MAX)
    		{
    			System.out.print
    				(
    					"Please enter a valid number: "
    				);
                            answer = scan.nextInt();
    		}
    should be
    Code:
    while(guess < 1 || guess > MAX)
    		{
    			System.out.print
    				(
    					"Please enter a valid number: "
    				);
                            guess = scan.nextInt();
    		}
    Reply With Quote Reply With Quote
  16. #6
    Stealther's Avatar
    Join Date
    Sep 2009
    Location
    In the code in the US
    Age
    16
    Posts
    82
    Rep Power
    3
  17. Stealther is on a distinguished road
  18. Re: Help with Guessing Game

    oh wow what a humiliating mistake that was!!!! But for some reason when I enter a number like 90 or 555 it still doesn't allow for input. I'm wondering why. Thanks anyway I'll try to ask my teacher on another school day.
    Reply With Quote Reply With Quote
  19. #7
    Stealther's Avatar
    Join Date
    Sep 2009
    Location
    In the code in the US
    Age
    16
    Posts
    82
    Rep Power
    3
  20. Stealther is on a distinguished road
  21. Re: Help with Guessing Game

    Oh hey just to let you guys know I got it fixed. The whole code is correct but it seems that I had to make a new Scanner and I did and it works extremely great! Thanks for your help the only thing with your is that I was not at your level with making my own classes and things like that but I will learn soon! :) Thanks for your help anyway! :)
    AP Computer Science should be a foreign language class
    Reply With Quote Reply With Quote

Similar Threads

  1. Game about Robots (classes)
    By omz in forum C and C++ Programming
    Replies: 9
    Last Post: 12-09-2007, 02:39 PM
  2. My Number Guessing Game!
    By Nick Presta in forum C and C++ Programming
    Replies: 19
    Last Post: 03-11-2007, 04:57 PM
  3. COTW :: Nick's Python Guessing Game
    By callumjones in forum News and Discussion
    Replies: 0
    Last Post: 10-27-2006, 06:01 AM