Results 1 to 6 of 6
- 09-29-2012, 02:24 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 15
- Rep Power
- 0
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
- 09-29-2012, 02:55 PM #2
Re: Do-while problem, or other problem?
Don't use the == comparison operator to compare Strings or any Objects. Use the .equals(...) method.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-29-2012, 06:15 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 15
- Rep Power
- 0
Re: Do-while problem, or other problem?
Thanks for that tip! That part is now working.
Edit was as follows:
I added a string so equals could compare the input to the stored string.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; } }
Now I'll work on the number of attempts, which is still going over 3.
-
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.
- 09-30-2012, 07:08 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 15
- Rep Power
- 0
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:
I've printed out the code and am trying to reason if I used the proper commands and position in the program. Thanks.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
- 09-30-2012, 07:22 PM #6
Member
- Join Date
- Sep 2012
- Posts
- 15
- Rep Power
- 0
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":
Anybody would like to confirm? Thanks.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
Similar Threads
-
Small problem with problem with Java, C++ parse program.
By dragstang86 in forum New To JavaReplies: 4Last Post: 10-30-2011, 03:43 AM -
Word problem...PROBLEM!
By aceoraiz in forum Threads and SynchronizationReplies: 6Last Post: 12-17-2010, 03:38 AM -
Can anyone see the problem with my code? problem understanding switch (newbish)
By keith in forum New To JavaReplies: 9Last Post: 09-21-2010, 04:15 PM -
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks