# Thread: Number Guessing Game issue

1. Member
Join Date
Nov 2011
Posts
41
Rep Power
0

## Number Guessing Game issue

I am trying to write a code that asks the user to enter a number between 1-1000. The program will keep asking the user to continue guessing until he gets it right, telling him if the guess is too high or too low. When the user guess the right number a message will display that he guessed the number, how many tries it took him and if he/she would like to play again. So far I have the code that keeps asking the user for a number, but I don't know how to incorporate another loop to repeat the first loop if the user wants to play again. Where in the code do I insert the second loop and do I use a another do..while loop or use for or if loops? Here is my code:

Java Code:
```import java.util.Scanner;

public class RandomGuess3 {
public static void main(String[] args) {
int secretNumber;
secretNumber = (int) (Math.random() * 999 + 1);
Scanner keyboard = new Scanner(System.in);
int guess;
do {
System.out.print("Enter a guess (1-1000): ");
guess = keyboard.nextInt();
if (guess == secretNumber)
else if (guess < secretNumber)
System.out.println("Your guess is smaller than the secret number.");
else if (guess > secretNumber)
System.out.println("Your guess is greater than the secret number.");
} while (guess != secretNumber);
}
}```

2. ## Re: Number Guessing Game issue

Originally Posted by gabrielpr12
...So far I have the code that keeps asking the user for a number, ...
You'll need to add an int counter variable that you increase inside of the while loop.

...but I don't know how to incorporate another loop to repeat the first loop if the user wants to play again. Where in the code do I insert the second loop and do I use a another do..while loop or use for or if loops?
Experiment with this -- you'll probably get it right, if not the first time, then soon. If you fail after several tries, show us your attempt.

3. Member
Join Date
Nov 2011
Posts
41
Rep Power
0

## Re: Number Guessing Game issue

Originally Posted by Fubarable
You'll need to add an int counter variable that you increase inside of the while loop.

Experiment with this -- you'll probably get it right, if not the first time, then soon. If you fail after several tries, show us your attempt.
But, if I add a counter I would have to create a for loop or something to that effect, my question is where do I put the second loop? I have to ask the user if they want to play again, and if the answer is yes, then I have to repeat the do..while loop.

4. ## Re: Number Guessing Game issue

Don't try to cram everything in one method. Suppose you have a method 'playGame()' that can play one single game. Also suppose you have a boolean method playAgain() that returns true if the user wants to play again. The following simple structure conducts everything:

Java Code:
```do {
playGame();
}
while (playAgain());```
kind regards,

Jos

5. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
9

## Re: Number Guessing Game issue

Maybe something like this:

Java Code:
```while(true) {

do {
game
}
while();

if (JOptionPane.showConfirmDialog(null, "Play again") != JOptionPane.OK_OPTION)
break;
}```

6. ## Re: Number Guessing Game issue

Originally Posted by diamonddragon
Maybe something like this:

Java Code:
```while(true) {

do {
game
}
while();

if (JOptionPane.showConfirmDialog(null, "Play again") != JOptionPane.OK_OPTION)
break;
}```
I disagree. Cleaner to use a controlling boolean variable.

7. Member
Join Date
Nov 2011
Posts
41
Rep Power
0

## Re: Number Guessing Game issue

I have changed the code, but now I keep going on a loop. After the user guesses correctly I ask if he wants to play again and it it supposed to accept input from the user, but instead it goes back to the do..while loop. Any ideas how do I break this loop in order to accept the user input?
Java Code:
```import java.util.Scanner;

public class RandomGuess3
{
public static void main(String[] args)
{

boolean playAgain = true;
do{
playGame();
}while (playAgain);
}
public static void playGame()
{
int secretNumber;
secretNumber = (int) (Math.random() * 999 + 1);
Scanner keyboard = new Scanner(System.in);
int guess;
boolean playAgain = true;

do {
System.out.print("Try to guess the secret number.\nType a number between 1-1000): ");
guess = keyboard.nextInt();
if (guess == secretNumber)
System.out.println("You guessed correctly! Congratulations!");
else if (guess < secretNumber)
System.out.println("The number you typed is lower than the secret number.");
else if (guess > secretNumber)
System.out.println("The number you typed is higher than the secret number.");
} while (guess != secretNumber);

if (guess == secretNumber)
System.out.println("Do you want to play again?");
playAgain = true;
else
playAgain = false;

}
}```

8. ## Re: Number Guessing Game issue

In my hint playAgain() was a method ... and it prompts the user and returns true if the user wants to play another game ...

kind regards,

Jos

9. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
9

## Re: Number Guessing Game issue

Originally Posted by Fubarable
I disagree. Cleaner to use a controlling boolean variable.
Than:

Java Code:
```boolean control = true;
while(control) {

do {
game
}
while();

if (JOptionPane.showConfirmDialog(null, "Play again") != JOptionPane.OK_OPTION)
control = false;
}```

10. Member
Join Date
Nov 2011
Posts
41
Rep Power
0

## Re: Number Guessing Game issue

Originally Posted by JosAH
In my hint playAgain() was a method ... and it prompts the user and returns true if the user wants to play another game ...

kind regards,

Jos
Now, if playAgain() is another method, can I reference the playGame() method to get the answer?

11. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
9

## Re: Number Guessing Game issue

Originally Posted by gabrielpr12
Now, if playAgain() is another method, can I reference the playGame() method to get the answer?
You don't reference method, but assign reference variable to method return type.

12. Member
Join Date
Nov 2011
Posts
41
Rep Power
0

## Re: Number Guessing Game issue

I changed the code again, but now when the user answers yes or no it does the same thing, it goes back to the main do..while loop. What am I missing in order to stop the loop? Is it something wrong with the boolean method playAgain()? Here is the updated code:

Java Code:
```import java.util.Scanner;

public class RandomGuess3
{
public static void main(String[] args)
{

boolean playAgain = true;
do{
playGame();
}while (playAgain());
}
public static void playGame()
{
int secretNumber;
secretNumber = (int) (Math.random() * 999 + 1);
Scanner keyboard = new Scanner(System.in);
int guess;

do {
System.out.print("Try to guess the secret number.\nType a number between 1-1000): ");
guess = keyboard.nextInt();
if (guess == secretNumber)
System.out.println("You guessed correctly! Congratulations!"+
"\n Do you want to play again?");
else if (guess < secretNumber)
System.out.println("The number you typed is lower than the secret number.");
else if (guess > secretNumber)
System.out.println("The number you typed is higher than the secret number.");
} while (guess != secretNumber);
}

public static boolean playAgain()
{
Scanner keyboard = new Scanner(System.in);
return true;
}
}```

13. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
9

## Re: Number Guessing Game issue

Try:
Java Code:
```if (answer.equals("y"))
return true;
else
return false;```
Last edited by diamonddragon; 01-24-2012 at 11:58 PM.

14. Member
Join Date
Nov 2011
Posts
41
Rep Power
0

## Re: Number Guessing Game issue

Originally Posted by diamonddragon
Try:
Java Code:
```if (answer.equals("y"))
return true;
else
return false;```
works perfectly! thanks a lot!!!
Last edited by gabrielpr12; 01-25-2012 at 12:05 AM.

15. Member
Join Date
Nov 2011
Posts
41
Rep Power
0

## Re: Number Guessing Game issue

Here is the final working code:
Java Code:
```import java.util.Scanner;

public class RandomGuess3
{
public static void main(String[] args)
{

boolean playAgain = true;
do{
playGame();
}while (playAgain());
}
public static void playGame()
{
int secretNumber;
secretNumber = (int) (Math.random() * 999 + 1);
Scanner keyboard = new Scanner(System.in);
int guess;

do {
System.out.print("Try to guess the secret number.\nType a number between 1-1000): ");
guess = keyboard.nextInt();
if (guess == secretNumber)
System.out.println("You guessed correctly! Congratulations!"+
"\n Do you want to play again?");
else if (guess < secretNumber)
System.out.println("The number you typed is lower than the secret number.");
else if (guess > secretNumber)
System.out.println("The number you typed is higher than the secret number.");
} while (guess != secretNumber);
}

public static boolean playAgain()
{
Scanner keyboard = new Scanner(System.in);
return true;
else
return false;
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•