Guessing a number help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KoreyAusTex
    New Member
    • Feb 2007
    • 36

    Guessing a number help.

    I have written a program using method calls as opposed to nested while loops, or rather a while within a while, but for some reason the only way I can get the program to terminate is to type break in the main method after the method calls. This is not the way I was supposed to do it, any thoughts?

    Code:
    import java.util.*;
    public class GuessNumber 
    {
        public static void main(String[] args) 
        {
           Scanner read = new Scanner(System.in);
           System.out.print("Do you want to play a game? ");//War Games!
           String collegeTry = read.next();
           collegeTry = collegeTry.toLowerCase();
           while (((collegeTry.equals("yeah")) || (collegeTry.equals("sure")) || (collegeTry.equals("ok"))))
           {
               game();
               break;
           }
        }
    
        public static void game() 
        {
            Scanner read = new Scanner(System.in);
            Random generator = new Random();
            int randNum = generator.nextInt(100) + 1; // pick a number between 1 & 100
            System.out.println("\nWelcome to the High-Low Guessing Game!");
            System.out.println("Try to guess the number I've chosen between 1 and 100.");
            System.out.print("\nWhat is your guess? ");
            int guessNum = read.nextInt();
            int count = 1;
            
            while (guessNum != randNum) 
            {
                if (guessNum > randNum)
                {
                    System.out.println(guessNum + " is too high.");
                }
                else
                {
                    System.out.println(guessNum + " is too low.");
                }
                count++;
                System.out.print("What is your guess? ");
                guessNum = read.nextInt();
            }
                
            System.out.println("\nCorrect! You only needed " + count + " guesses to get it!");    
        }
        
    }
    Also, can this be done using a for loop? As always I appreciate your advice and guidance!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Look at your main() method: you only read collegeTry once before the while loop
    starts and it keeps its value (it is never changed), so if the value is one of the
    affirmative values the while loop keeps on looping.

    kind regards,

    Jos

    Comment

    • KoreyAusTex
      New Member
      • Feb 2007
      • 36

      #3
      Originally posted by JosAH
      Look at your main() method: you only read collegeTry once before the while loop
      starts and it keeps its value (it is never changed), so if the value is one of the
      affirmative values the while loop keeps on looping.

      kind regards,

      Jos
      Not sure what you meant, but I think I might have fixed it? The only thing I am having trouble with is when I enter -1 as a guess, it should print the message and then terminate, but I think it has something to do with boolean and int types not working together? Any suggestions?

      Code:
      import java.util.*;
      public class GuessNumber 
      {
          public static void main(String[] args) 
          {
             Scanner read = new Scanner(System.in);
             System.out.print("Do you want to play a game? ");//War Games!
             String collegeTry = read.next();
             collegeTry = collegeTry.toLowerCase();
             while (((collegeTry.equals("yeah")) || (collegeTry.equals("sure")) || (collegeTry.equals("ok"))))
             {
                 game();
                 System.out.print("\nDo you want to play a game? ");
                 collegeTry = read.next();
                 collegeTry = collegeTry.toLowerCase();
             }
          }
      
          public static void game() 
          {
              Scanner read = new Scanner(System.in);
              Random gen = new Random();
              int randNum = gen.nextInt(100) + 1; // pick a number between 1 & 100
              System.out.println("\nWelcome to the High-Low Guessing Game!" + 
              "Try to guess the number I've chosen between 1 and 100.");
              System.out.print("\nWhat is your guess? ");
              int guessNum = read.nextInt();
              int count = 1;
              
              while (guessNum != randNum) 
              {
                  if(guessNum > randNum)
                  {
                      System.out.println(guessNum + " is too high.");
                  }
                  else if(guessNum < randNum)
                  {
                      System.out.println(guessNum + " is too low.");
                  }
                  else if(guessNum == -1)
                  {
                      System.out.println("You stopped the game after " + count + " guess(es)!");
                  }
                  
                  count++;
                  System.out.print("What is your guess? ");
                  guessNum = read.nextInt();
              }
                  
              System.out.println("Correct! You only needed " + count + " guesses to get it!");    
          }
          
      }

      Comment

      • trdeepak
        New Member
        • Mar 2008
        • 2

        #4
        Try using

        try{

        } catch

        block

        Comment

        • KoreyAusTex
          New Member
          • Feb 2007
          • 36

          #5
          Originally posted by trdeepak
          Try using

          try{

          } catch

          block
          Actually you are using concepts we have yet to learn

          Comment

          • nomad
            Recognized Expert Contributor
            • Mar 2007
            • 664

            #6
            Originally posted by KoreyAusTex
            Not sure what you meant, but I think I might have fixed it? The only thing I am having trouble with is when I enter -1 as a guess, it should print the message and then terminate, but I think it has something to do with boolean and int types not working together? Any suggestions?

            Code:
            import java.util.*;
            public class GuessNumber 
            {
                public static void main(String[] args) 
                {
                   Scanner read = new Scanner(System.in);
                   System.out.print("Do you want to play a game? ");//War Games!
                   String collegeTry = read.next();
                   collegeTry = collegeTry.toLowerCase();
                   while (((collegeTry.equals("yeah")) || (collegeTry.equals("sure")) || (collegeTry.equals("ok"))))
                   {
                       game();
                       System.out.print("\nDo you want to play a game? ");
                       collegeTry = read.next();
                       collegeTry = collegeTry.toLowerCase();
                   }
                }
            
                public static void game() 
                {
                    Scanner read = new Scanner(System.in);
                    Random gen = new Random();
                    int randNum = gen.nextInt(100) + 1; // pick a number between 1 & 100
                    System.out.println("\nWelcome to the High-Low Guessing Game!" + 
                    "Try to guess the number I've chosen between 1 and 100.");
                    System.out.print("\nWhat is your guess? ");
                    int guessNum = read.nextInt();
                    int count = 1;
                    
                    while (guessNum != randNum) 
                    {
                        if(guessNum > randNum)
                        {
                            System.out.println(guessNum + " is too high.");
                        }
                        else if(guessNum < randNum)
                        {
                            System.out.println(guessNum + " is too low.");
                        }
                        else if(guessNum == -1)
                        {
                            System.out.println("You stopped the game after " + count + " guess(es)!");
                        }
                        
                        count++;
                        System.out.print("What is your guess? ");
                        guessNum = read.nextInt();
                    }
                        
                    System.out.println("Correct! You only needed " + count + " guesses to get it!");    
                }
                
            }
            On the part:
            Do you want to play a game?
            you might want to print on the screen enter yeah, sure or ok. If you don't that person will never get in.

            Comment

            • nomad
              Recognized Expert Contributor
              • Mar 2007
              • 664

              #7
              Originally posted by KoreyAusTex
              Not sure what you meant, but I think I might have fixed it? The only thing I am having trouble with is when I enter -1 as a guess, it should print the message and then terminate, but I think it has something to do with boolean and int types not working together? Any suggestions?

              Code:
              import java.util.*;
              public class GuessNumber 
              {
                  public static void main(String[] args) 
                  {
                     Scanner read = new Scanner(System.in);
                     System.out.print("Do you want to play a game? ");//War Games!
                     String collegeTry = read.next();
                     collegeTry = collegeTry.toLowerCase();
                     while (((collegeTry.equals("yeah")) || (collegeTry.equals("sure")) || (collegeTry.equals("ok"))))
                     {
                         game();
                         System.out.print("\nDo you want to play a game? ");
                         collegeTry = read.next();
                         collegeTry = collegeTry.toLowerCase();
                     }
                  }
              
                  public static void game() 
                  {
                      Scanner read = new Scanner(System.in);
                      Random gen = new Random();
                      int randNum = gen.nextInt(100) + 1; // pick a number between 1 & 100
                      System.out.println("\nWelcome to the High-Low Guessing Game!" + 
                      "Try to guess the number I've chosen between 1 and 100.");
                      System.out.print("\nWhat is your guess? ");
                      int guessNum = read.nextInt();
                      int count = 1;
                      
                      while (guessNum != randNum) 
                      {
                          if(guessNum > randNum)
                          {
                              System.out.println(guessNum + " is too high.");
                          }
                          else if(guessNum < randNum)
                          {
                              System.out.println(guessNum + " is too low.");
                          }
                          else if(guessNum == -1)
                          {
                              System.out.println("You stopped the game after " + count + " guess(es)!");
                          }
                          
                          count++;
                          System.out.print("What is your guess? ");
                          guessNum = read.nextInt();
                      }
                          
                      System.out.println("Correct! You only needed " + count + " guesses to get it!");    
                  }
                  
              }
              try this instead...
              if(guessNum == -1);

              also you will need to stop the program some how after you type -1
              and move this
              System.out.prin tln("Correct! You only needed " + count + " guesses to get it!");
              somewhere...


              Do you know why?


              good luck


              nomad

              Comment

              • KoreyAusTex
                New Member
                • Feb 2007
                • 36

                #8
                Originally posted by nomad
                try this instead...
                if(guessNum == -1);

                also you will need to stop the program some how after you type -1
                and move this
                System.out.prin tln("Correct! You only needed " + count + " guesses to get it!");
                somewhere...


                Do you know why?


                good luck


                nomad
                Do I know why what? That I have to move it?

                Comment

                • nomad
                  Recognized Expert Contributor
                  • Mar 2007
                  • 664

                  #9
                  Originally posted by KoreyAusTex
                  Do I know why what? That I have to move it?

                  why you need to do this
                  if(guessNum == -1);
                  in stead of this
                  else if(guessNum == -1);


                  Did you do a test on your program. I think it's still not right.
                  What do you need to move is for you to figure out.


                  I probably should have made you figure it out by giving you hints.

                  nomad

                  Comment

                  • KoreyAusTex
                    New Member
                    • Feb 2007
                    • 36

                    #10
                    Originally posted by nomad
                    why you need to do this
                    if(guessNum == -1);
                    in stead of this
                    else if(guessNum == -1);


                    Did you do a test on your program. I think it's still not right.
                    What do you need to move is for you to figure out.


                    I probably should have made you figure it out by giving you hints.

                    nomad
                    Sorry I think you misunderstood me, I got confused, I understand I need to move it out, but I wasn't clear on what you were referring too.

                    Comment

                    Working...