# Do-while problem, or other problem?

• 09-29-2012, 02:24 PM
pakupakuman
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:

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!
• 09-29-2012, 02:55 PM
DarrylBurke
Re: Do-while problem, or other problem?
Don't use the == comparison operator to compare Strings or any Objects. Use the .equals(...) method.

db
• 09-29-2012, 06:15 PM
pakupakuman
Re: Do-while problem, or other problem?
Thanks for that tip! That part is now working.

Edit was as follows:

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.
• 09-29-2012, 06:51 PM
Fubarable
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
pakupakuman
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:

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.
• 09-30-2012, 07:22 PM
pakupakuman
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":

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.