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:
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;
String answer;
do {
System.out.print("Enter a guess (1-1000): ");
guess = keyboard.nextInt();
if (guess == secretNumber)
System.out.println("Your guess is correct. Congratulations!");
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);
}
}
Re: Number Guessing Game issue
Quote:
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.
Quote:
...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.
Re: Number Guessing Game issue
Quote:
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.
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:
Code:
do {
playGame();
}
while (playAgain());
kind regards,
Jos
Re: Number Guessing Game issue
Maybe something like this:
Code:
while(true) {
do {
game
}
while();
if (JOptionPane.showConfirmDialog(null, "Play again") != JOptionPane.OK_OPTION)
break;
}
Re: Number Guessing Game issue
Quote:
Originally Posted by
diamonddragon
Maybe something like this:
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.
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?
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;
String answer;
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?");
answer = keyboard.nextLine();
if(answer =="y")
playAgain = true;
else
playAgain = false;
}
}
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
Re: Number Guessing Game issue
Quote:
Originally Posted by
Fubarable
I disagree. Cleaner to use a controlling boolean variable.
Than:
Code:
boolean control = true;
while(control) {
do {
game
}
while();
if (JOptionPane.showConfirmDialog(null, "Play again") != JOptionPane.OK_OPTION)
control = false;
}
Re: Number Guessing Game issue
Quote:
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?
Re: Number Guessing Game issue
Quote:
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.
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:
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()
{
String answer;
Scanner keyboard = new Scanner(System.in);
answer = keyboard.nextLine();
if (answer =="y");
return true;
}
}
Re: Number Guessing Game issue
Try:
Code:
if (answer.equals("y"))
return true;
else
return false;
Re: Number Guessing Game issue
Quote:
Originally Posted by
diamonddragon
Try:
Code:
if (answer.equals("y"))
return true;
else
return false;
works perfectly! thanks a lot!!!
Re: Number Guessing Game issue
Here is the final working code:
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()
{
String answer;
Scanner keyboard = new Scanner(System.in);
answer = keyboard.nextLine();
if (answer.equals("y"))
return true;
else
return false;
}
}