Results 1 to 6 of 6
  1. #1
    mbnumba6 is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default Gueesing Game Almost done, but not working correctly

    Hi, I am making a guessing game from the java language. The user is asked to input a number. The number is 1 - a random max number. If the number guessed is correct, then Play should return the number of guesses. If the number guessed is null (the player clicked Cancel), the user should get a “Thanks for trying” message, and Play should return -1. If the number is too high, the player should get a message telling them that the number is too high and should get to try again. If the number is too low, the player should get a message telling them that the number is too low, and they should get to try again.

    The problem is when I input a number my compiler will read it correctly. It will notify me that I entered a number too low or too high, but it wont let me try again unless I start the whole program over. I feel that I need to make some sort of loop to get this done, but how?



    Java Code:
    import java.util.*;
    
    public class GuessingGame
    {   
        int maxNumber;    
    /**
     * Constructor setting the input number
     * and the max number
     * @param int num
     */
         public GuessingGame(int num)
         {
           String number = "";
           maxNumber = num;
         }
    
    /**
    * Random number between 1 and max is generated for the 
    * user to guess. User is asked to guess a number. If number
    * guessed was correct then return the number of guesses.
    */
            public void Play()
            {
              Random ranNum = new Random(); 
              int correctNum = ranNum.nextInt(maxNumber) + 1; //the correct number guessed equals the max random number
              Scanner in = new Scanner(System.in);  
              System.out.println("Guess a number");
               int number = in.nextInt();
              
              
              if(number == correctNum) //If number correct, return number of guesses
              {
                System.out.println(in.nextInt() + " good guess");
                  
              }
    /**
    * if number guessed is null or user quits then 
     * return message stating "thanks for playing" and -1
     * @return
     */
              else if (number == -1) 
              {
                System.out.println("Thanks for trying" + -1);   
              }
    /**
     * if number guessed is too high,
     * then return message stating number is too high
     * and to try again
     * @return
     */
              else if (number > correctNum) 
              {
                  System.out.println("The number is too high, try again");
              }
    /**
     * if number guessed too low, then 
     * return message stating number is too low
     * and to try again
     * @return 
     */
              else if (number < correctNum)
              {
                 System.out.println("The number is too low, please try again");   
              }
              
              
            }
    }
    Last edited by mbnumba6; 03-17-2009 at 02:22 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Look here:
    Java Code:
        int number = in.nextInt();
        if (in.nextInt() == correctNum) 
        {
    what happens to "number" here? Where do you actually use this value?

    Also, you need to have some type of loop that will let the user keep trying until he quits or runs out of turns. What type of loop should you use?

    I would also re-define your use of the term "almost done".
    Last edited by Fubarable; 03-17-2009 at 02:14 AM.

  3. #3
    mbnumba6 is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    Ok, so I should change in.nextInt() to number right? Ok I did that already. I edited my post. And maybe you are right I should re-define my use of the term "almost done".

    Also I have no idea on how I should get this loop to work. I tried this loop

    while(number <= correctNum)

    then increment number. But it did not work right

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Yep, you're right: it's a while loop that you need. So first of all, let's assume that each iteration through the while loop is one "guess". If so, then you should only get one valid guess each time the user goes through the loop and that valid guess (in.nextInt()) should be placed in "number". Note that I said valid guess in that sometimes you have to keep prompting the user until the number they enter is valid -- is an int, is within the range desired, etc...

    Your loop's stopping condition could be a boolean variable such as
    Java Code:
    boolean done = false;
    while (!done)
    {

  5. #5
    mbnumba6 is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    umm. Ok if I would make a while loop like that, would I have to
    somehow say that when the user makes the correct guess than
    that would equal done?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If the user makes a correct guess, or if the user quits, then you simply set done = true;

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. Help with sphere.java - not coming out correctly
    By JavaInLove in forum Java 2D
    Replies: 3
    Last Post: 03-17-2009, 08:38 PM
  3. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  4. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 04:45 PM
  5. How Do I Embed Java Correctly To A Web Page
    By abcd in forum Java Applets
    Replies: 7
    Last Post: 01-28-2008, 07:53 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
  •