Guessing game almost done, but not working correctly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • falconsx23
    New Member
    • Nov 2008
    • 55

    Guessing 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 through a random max number. If the number guessed is correct, then the program 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 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 user 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?



    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");   
              }
              
              
            }
    }
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    When you use in.nextInt(), it jumps one number further each time, so if you do [code=java]if(in.nextInt() == 1){System.out.p rint(1);}
    if(in.nextInt() == 2){System.out.p rint(2);}
    if(in.nextInt() == 2){System.out.p rint(3);}[/code] and just input a 3, it will be expecting two further inputs.

    What you want to do is more like this: [code=java]int nextInt = in.nextInt();
    if(nextInt == 1){System.out.p rint(1);}
    if(nextInt == 2){System.out.p rint(2);}
    if(nextInt == 2){System.out.p rint(3);}[/code] Greetings,
    Nepomuk

    Comment

    • falconsx23
      New Member
      • Nov 2008
      • 55

      #3
      I am sorry, but I am not understanding what you are trying to say.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        OK, imagine the input as a stream of numbers separated by white spaces, e.g. 3 2 6 21 5
        Now, in.nextInt() will read the first number in the stream and remove it. So with
        Code:
        if(in.nextInt() == 3){System.out.println(3);}
        if(in.nextInt() > 2){System.out.println("Bigger");}
        else {System.out.println("Not bigger");}
        it will print a '3' and "Not bigger" (because it's comparing the 2 now) and leave the stream 6 21 5
        You may notice, that there's a further 3 in that stream. The program doesn't (and shouldn't) care at this moment.

        Now, you want to check the same number a few times. So make sure you save it:
        Code:
        int nextInt = in.nextInt();
        and work with that instead of reading the next number in line again and again and again.

        Greetings,
        Nepomuk

        Comment

        • adamrehill
          New Member
          • Mar 2009
          • 7

          #5
          I would just add

          int number = in.nextInt();

          after you state that the guess was too low or too high. So for example:

          Code:
          else if (number > correctNum)  
                    { 
                        System.out.println("The number is too high, try again");
                        int number = in.nextInt();
                    }
          and

          Code:
          else if (number < correctNum) 
                    { 
                       System.out.println("The number is too low, please try again");
                       int number = in.nextInt();    
                    }
          that way you are requiring the user or player to enter a new number (guess) each time they are either too high or too low.

          I think that would work anyways...

          Adam

          Comment

          • falconsx23
            New Member
            • Nov 2008
            • 55

            #6
            Ok I understand what you are saying, but what do you mean by "make sure you save it."

            Comment

            • falconsx23
              New Member
              • Nov 2008
              • 55

              #7
              Ok I believe I have it now. I did make a while loop and inside that while loop I also had the number of guesses incremented. So the program would also have output how many times the user tried to guess the correct number.


              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) 
                   { 
                     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 int Play() 
                      { 
                        Random ranNum = new Random();  
                        int correctNum = ranNum.nextInt(maxNumber) + 1; //the correct number guessed equals the max random number 
                        int number = 0;
                        int numGuess = 0;
                        
                        while( number != correctNum)
                        {
                            
                          Scanner in = new Scanner(System.in); 
                      
                          System.out.println("Guess a number"); 
                          number = in.nextInt(); 
                          
                          if(number == correctNum) //If number correct, return number of guesses 
                          { 
                          System.out.println("Good guess"); 
                          ++ numGuess;
                          return numGuess;
                
                          } 
              /** 
              * 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 ");  
                              return -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"); 
                                ++ numGuess;
                                
                          }     
              /** 
               * 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");    
                               ++ numGuess;
                          } 
                
                
                      } 
                      return -1; // should never get here
                  }
              }

              Comment

              Working...