Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with Java Guessing game?

    Hello, I'm new here. ^^ I hope I can get something out of registering here haha

    Ok, so, in our Java Programming class, we're writing a program that randomly generates a number between 1 and 100, and the user must guess what that number is. The program so far works, and it gives hints like "Too high" or "too low" and it counts the number of tries taken to reach the correct number, which is displayed in a message that says they guessed correctly.

    Well, now we have to add a few features to the program so it does these 3 things:

    1) When the user enters a number that is not between 1 and 100, it will give an error message and not count that guess in the number of tries to guess the correct number, and they can enter a valid number after the error message is displayed.

    2) After the game is over, the user should have the option to play again. It should say something along the lines of "Play again? (Y or N)" and if the user answers with 'Y', the game should restart and if the user answers with a 'N', the loop should end.

    3) If the user chooses to replay the game, the guess counter should reset back to 0.

    I'm just not exactly sure how to get those above standards...

    Here is the code:

    Code:
    import cs1.Keyboard;
    import java.util.Random;
    
    public class GuessingGame2
    {
    	public static void main (String[] args)
    	{
    		Random generator = new Random();
    		int num;
    		int guess;
    		int tries = 0;
    		char replay;
    
    		num = generator.nextInt(100) + 1;
    
    		System.out.println("Welcome to the Guessing Game!!");
    		System.out.println();
    		System.out.println();
    
    		do
    		{
    			System.out.print("Please enter a number between 1 and 100: ");
    			guess = Keyboard.readInt();
    
    			tries++;
    
    
    			if(guess>num)
    			{
    				System.out.println("Too high. Try again.");
    				System.out.println();
    			}
    			if(guess<num)
    			{
    				System.out.println("Too low. Try again.");
    				System.out.println();
    			}
    			if(guess==num)
    			{
    				System.out.println("Correct! It took you " + tries + " tries to guess the number!");
    			}
    
    		}while(guess!=num);
    	}
    }
    Any help is greatly appreciated! ^^

    Thanks!
    Last edited by buddygirrl234; 11-13-2008 at 06:49 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Since this is for school we can only give you ideas or help you modify you're code while keeping it intact. We can't give you additional code.
    You'd doing fine so far though, its very good.

    For you're first quest, add an if statement after you've read the newest integer from the keyboard. If its within range, then check you're other ifs and increment you're tries. Otherwise, just display a message telling them its wrong and set you're guess to -1 or something that will be out of range.

    For you're second question, wrap all of you're functional code (don't forget the one that generates the number!) into a either a do/while loop (while they want to replay), or a pre-conditioned while loop. Scan in their choice (and remember to ask again if they don't enter a 'y' or 'n'.

    As for you're last question, simply reinitialize all of you're variables within the loop. That way they will always start fresh.

    Hope that helps!
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    buddygirrl234 (11-13-2008)

  • #3
    New Coder
    Join Date
    Nov 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    1) When the user enters a number that is not between 1 and 100, it will give an error message and not count that guess in the number of tries to guess the correct number, and they can enter a valid number after the error message is displayed.

    2) After the game is over, the user should have the option to play again. It should say something along the lines of "Play again? (Y or N)" and if the user answers with 'Y', the game should restart and if the user answers with a 'N', the loop should end.

    3) If the user chooses to replay the game, the guess counter should reset back to 0.
    The best way to analyze these situations is to get all stipulations that need to be satisfied..

    1) If the number is not in between 1-100 you want the following to happen:

    Create an if statement to see if the entered number is within the range of accepted numbers, if it is not then display a message and continue on with the loop

    If this occurs after tries is incremented you want to decrement tries.

    2) After the do while loop is satisfied, then you need to satisfy two events,
    either the user will choose to play again or decline..
    Create a variable to store their answer
    Prompt them to select 'y' or 'n' and then do another if statement that satisfies their answer.
    You also might want to set up a delay for this message or a alert box so the user has time to read the message before they are asked to play again. Might get you some extra credit when you do some simple little tweaks the professor doesn't ask for or request.. Should only take a few minutes of research to do..

    3)Set the tries counter to 0. Simple enough. Re-Set the value to 0.


    Also, you might want to initialize your variables that are before your do-loop, it's good programming practice.
    One more thing you might want to think about making your do while loop a method. Or wrap it like fou-lu said...

    Hope this helps, if you have any other questions please ask.
    As Fou-Lu stated, we won't do your work! But were always happy to provide you with the stepping stones to get there! Good luck
    Last edited by TechGlider; 11-14-2008 at 09:37 PM.


  •  

    Posting Permissions

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