Results 1 to 6 of 6
  1. #1
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Question Do-while problem, or other problem?

    Hello -

    Learning Java, I'm converting a simple guess-the-number game from C++ to Java. As the code stands, the basics are working. You have three tries to guess a number, and the computer will let you know if your guess is too low or too high.

    i think that the problem is in the "play again y/n" part of the program. regardless of your selection, it will continue to play, and the "number of attempts" counter doesn't reset.

    Here's the code:

    Java Code:
    /*
     * Filename: 	GuessTheNumber.java
     * Created: 	9-28-2012 by MH
     *
     * Purpose: 	Conversion of Guess the Number Game from C++ to Java
     *
     */
    
    import javax.swing.*;     // GUI
    import java.util.Random;  // Randomizer
    
    public class GuessTheNumber
    {
    
       //main method
       public static void main ( String[] args )
       {
    
          //declare and initialize variables
          int compnum, yournum, tries;
          String selectyn, openingMessage, instructionMessage, attemptMessage, yournumString, windowMessage;
    
          //display opening messages
          openingMessage  = "Find and defuse the bomb!";
          JOptionPane.showMessageDialog( null, openingMessage );
          selectyn = "y";
    
          // game routine
          do
          {
              instructionMessage  = "Guess a number from 1 to 10.  You have three tries.";
              JOptionPane.showMessageDialog( null, instructionMessage );
    
    
              Random rand = new Random();        // random object
              compnum = rand.nextInt(10)+1;  // random number from 1 to 10
    
              tries = 0;
    	  while (tries < 4)
    	  {
                  tries++;
    
                  // get required input using dialog boxes
                  attemptMessage = "Attempt number " + tries + ":";
                  yournumString = JOptionPane.showInputDialog ( attemptMessage );
                  yournum = Integer.parseInt( yournumString );//convert string to integer
    
    	      // determine if numbers match
    	      if (compnum == yournum)
                  {
                      //congratulations, play again?
    		  windowMessage = "Conrgatulations!!! \n";
                      JOptionPane.showMessageDialog( null, windowMessage );
    		  PlayAgain();
                  }
    	      else
    		 {
    		 if ((compnum > yournum) && (tries < 3))
    		    {
                           windowMessage = "Uh oh.  The secret number is higher. \n";
                           JOptionPane.showMessageDialog( null, windowMessage );
    
                        }
                        else
                           {
                              if ((compnum < yournum) && (tries < 3))
                                 {
                                     windowMessage = "Uh oh.  The secret number is lower. \n";
                                     JOptionPane.showMessageDialog( null, windowMessage );
                                 }
                           }
                     }
    
                  if ((tries ==3) && (compnum != yournum))
                     {
                           windowMessage = "KABOOOM!!!  The secret number was " + compnum + ". \n\n";
                           JOptionPane.showMessageDialog( null, windowMessage );
    		       PlayAgain();
                     }
               }
    
       }  // end do
       while (selectyn == "y");
       System.exit(0);
    } // end main
    
    
    public static void PlayAgain()
       {
          int tries;
          String selectyn, selectynMessage;
          selectynMessage = "Would you like to play again (y/n)?";
          selectyn = JOptionPane.showInputDialog( selectynMessage );
          if (selectyn == "n")
             {
             System.exit(0);         
             }
                else
                   {
                      tries = 0;
                   }
       }
                
    } // end class

    Can somebody please take a stabat it and tell me where I'm messing it up? I know it's got to be something simple, but I can't see it. Thanks!
    Last edited by pakupakuman; 09-29-2012 at 05:51 PM. Reason: code format

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Do-while problem, or other problem?

    Don't use the == comparison operator to compare Strings or any Objects. Use the .equals(...) method.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: Do-while problem, or other problem?

    Thanks for that tip! That part is now working.

    Edit was as follows:

    Java Code:
    public static void PlayAgain()
       {
          int tries;
          String ynNoSelected = "n";
          String selectyn, selectynMessage;
          selectynMessage = "Would you like to play again (y/n)?";
          selectyn = JOptionPane.showInputDialog( selectynMessage );
          if (selectyn.equals(ynNoSelected))
             {
             System.exit(0);         
             }
                else
                   {
                      tries = 0;
                   }
       }
    I added a string so equals could compare the input to the stored string.

    Now I'll work on the number of attempts, which is still going over 3.

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

    Default Re: Do-while problem, or other problem?

    I don't see a loop in your code above. If you're still stuck, please post the relevant code.

  5. #5
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: Do-while problem, or other problem?

    Hello -

    Still stuck here. The "play again" works if you wish to exit, but the number of tries does not immediately reset (keeps on going).

    Here's what I have right now:

    Java Code:
    /*
     * Filename: 	GuessTheNumber.java
     * Created: 	9-28-2012 by MH
     *
     * Purpose: 	Conversion of Guess the Number Game from C++ to Java
     *
     */
    
    import javax.swing.*;     // GUI
    import java.util.Random;  // Randomizer
    
    public class GuessTheNumber
    {
    
       //main method
       public static void main ( String[] args )
       {
    
          //declare and initialize variables
          int compnum, yournum, tries;
          String selectyn, openingMessage, instructionMessage, attemptMessage, yournumString, windowMessage;
    
          //display opening messages
          openingMessage  = "Find and defuse the bomb!";
          JOptionPane.showMessageDialog( null, openingMessage );
          selectyn = "y";
    
          // game routine
          do
          {
              instructionMessage  = "Guess a number from 1 to 10.  You have three tries.";
              JOptionPane.showMessageDialog( null, instructionMessage );
    
    
              Random rand = new Random();        // random object
              compnum = rand.nextInt(10)+1;  // random number from 1 to 10
    
              tries = 0;
    	  while (tries < 4)
    	  {
                  tries++;
    
                  // get required input using dialog boxes
                  attemptMessage = "Attempt number " + tries + ":";
                  yournumString = JOptionPane.showInputDialog ( attemptMessage );
                  yournum = Integer.parseInt( yournumString );//convert string to integer
    
    	      // determine if numbers match
    	      if (compnum == yournum)
                  {
                      //congratulations, play again?
    		  windowMessage = "Conrgatulations!!! \n";
                      JOptionPane.showMessageDialog( null, windowMessage );
    		  PlayAgain();
                  }
    	      else
    		 {
    		 if ((compnum > yournum) && (tries < 3))
    		    {
                           windowMessage = "Uh oh.  The secret number is higher. \n";
                           JOptionPane.showMessageDialog( null, windowMessage );
    
                        }
                        else
                           {
                              if ((compnum < yournum) && (tries < 3))
                                 {
                                     windowMessage = "Uh oh.  The secret number is lower. \n";
                                     JOptionPane.showMessageDialog( null, windowMessage );
                                 }
                           }
                     }
    
                  if ((tries ==3) && (compnum != yournum))
                     {
                           windowMessage = "KABOOOM!!!  The secret number was " + compnum + ". \n\n";
                           JOptionPane.showMessageDialog( null, windowMessage );
    		       PlayAgain();
                     }
               }
    
       }  // end do
         while (selectyn.equals("y"));
         System.exit(0);
    } // end main
    
    
    public static void PlayAgain()
       {
            int tries;
            String ynNoSelected = "n";
            String selectyn, selectynMessage;
            selectynMessage = "Would you like to play again (y/n)?";
            selectyn = JOptionPane.showInputDialog( selectynMessage );
            if (selectyn.equals(ynNoSelected))
               {
             System.exit(0);         
               }
                  else
                     {
                        tries = 0;
                     }
       }
                
    } // end class
    I've printed out the code and am trying to reason if I used the proper commands and position in the program. Thanks.

  6. #6
    pakupakuman is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: Do-while problem, or other problem?

    I believe I solved it!

    I eliminated lines 90, 99-102, and changed line 39 from "tries < 4" to "tries < 3":

    Java Code:
    /*
     * Filename: 	GuessTheNumber.java
     * Created: 	9-28-2012 by MH
     *
     * Purpose: 	Conversion of Guess the Number Game from C++ to Java
     *
     */
    
    import javax.swing.*;     // GUI
    import java.util.Random;  // Randomizer
    
    public class GuessTheNumber
    {
    
       //main method
       public static void main ( String[] args )
       {
    
          //declare and initialize variables
          int compnum, yournum, tries;
          String selectyn, openingMessage, instructionMessage, attemptMessage, yournumString, windowMessage;
    
          //display opening messages
          openingMessage  = "Find and defuse the bomb!";
          JOptionPane.showMessageDialog( null, openingMessage );
          selectyn = "y";
    
          // game routine
          do
          {
              instructionMessage  = "Guess a number from 1 to 10.  You have three tries.";
              JOptionPane.showMessageDialog( null, instructionMessage );
    
    
              Random rand = new Random();        // random object
              compnum = rand.nextInt(10)+1;  // random number from 1 to 10
    
              tries = 0;
    	  while (tries < 3)
    	  {
                  tries++;
    
                  // get required input using dialog boxes
                  attemptMessage = "Attempt number " + tries + ":";
                  yournumString = JOptionPane.showInputDialog ( attemptMessage );
                  yournum = Integer.parseInt( yournumString );//convert string to integer
    
    	      // determine if numbers match
    	      if (compnum == yournum)
                  {
                      //congratulations, play again?
    		  windowMessage = "Conrgatulations!!! \n";
                      tries = 0;
                      JOptionPane.showMessageDialog( null, windowMessage );
    		  PlayAgain();
                  }
    	      else
    		 {
    		 if ((compnum > yournum) && (tries < 3))
    		    {
                           windowMessage = "Uh oh.  The secret number is higher. \n";
                           JOptionPane.showMessageDialog( null, windowMessage );
    
                        }
                        else
                           {
                              if ((compnum < yournum) && (tries < 3))
                                 {
                                     windowMessage = "Uh oh.  The secret number is lower. \n";
                                     JOptionPane.showMessageDialog( null, windowMessage );
                                 }
                           }
                     }
    
                  if ((tries ==3) && (compnum != yournum))
                     {
                           windowMessage = "KABOOOM!!!  The secret number was " + compnum + ". \n\n";
                           JOptionPane.showMessageDialog( null, windowMessage );
    		       PlayAgain();
                     }
               }
    
       }  // end do
         while (selectyn.equals("y"));
         System.exit(0);
    } // end main
    
    
    public static void PlayAgain()
       {
            String ynNoSelected = "n";
            String selectyn, selectynMessage;
            selectynMessage = "Would you like to play again (y/n)?";
            selectyn = JOptionPane.showInputDialog( selectynMessage );
            if (selectyn.equals(ynNoSelected))
               {
             System.exit(0);         
               }
       }
                
    } // end class
    Anybody would like to confirm? Thanks.

Similar Threads

  1. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  2. Word problem...PROBLEM!
    By aceoraiz in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-17-2010, 03:38 AM
  3. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 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
  •