Hi guys,
This is my first thread.
I have the following problem to resolve. I was able to do it within one method, but the requirements are different.
The program should pick a random number between one and ten (inclusive) and prompt the user for their guess. For each guess made, the program should tell the user if the guess was too high, too low, or correct. The user should only have four (4) tries to get it right before they lose the game.
When a game is over, a dialog should announce if they won or lost and ask the user if they want to play again. Your dialog should have yes and no buttons. If they lost this dialog box must show them what the correct answer was.
Requirements :
- All user input and output should be done using javax.swing.JOptionpane class
- The program should use the class java.util.Random to generate the numbers
- Your code must have the following methods at least:
public static void main ( String [] args ) This method is responsible for the "Play again?" logic, including the you won/you lost dialog. This means a loop that runs at least once, and continues until the player quits.
static boolean playGame ( ??? ) This method is responsible for playing one complete game each time it is called, including the what is your guess? input dialog. Note the message displayed in the input dialog changes each time through the loop, to show if the user's last guess was too high or too low. The method should return true if the user won. If they don't guess correctly after four tries, the user has lost and the method should return false.
static int compareTo ( ???, ??? ) This method compares the user input (a single guess) with the correct answer. It returns a negative integer if the guess is too low, a positive integer if the guess is too high, and 0 (zero) if the guess is correct.
So far I am stuck with this code:
import javax.swing.*;
import java.util.*;
public class GuessingGame
{
public static void main (String [] args)
{
//This method is responsible for the "Play again?" logic, including the you won / you lost dialog
//This means a loop that runs at least once, and continues until the player quits
boolean x =false;
boolean win = false;
do
{
playGame(x);
}
while (win == false);
JOptionPane.showInternalConfirmDialog(null,"please choose one", "information",JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}
public static boolean playGame (boolean choice)
{
//This method is responsible for playing one complete game each time it is called, including what's your guess
//input dialog
final int TOTALGUESS = 4; //Number of total guesses the user has.
int guessCount = 0; //Number of guesses the user has made
int rndNumber; // A random number picked by the computer.
Random rndNumbers = new Random(); //Generate random number.
rndNumber = rndNumbers.nextInt(10);
//Create input dialog
while (guessCount <= TOTALGUESS)
{
// A number entered by user as a guess.
String guess = JOptionPane.showInputDialog (null, "What's your guess? (1-10)", "Guessing Game", JOptionPane.QUESTION_MESSAGE);
// From string to int
int intGuess = Integer.parseInt(guess);
compareTo (intGuess, rndNumber); //Call method to compare guess with random.
guessCount++;
}
return choice;
}
public static int compareTo (int num1, int num2)
{
String message;
//This method compare the user input with the correct answer
if (num1 == num2) {
message = JOptionPane.showInputDialog(null, "Well done! Your choice was right!", "Guessing Game",JOptionPane.QUESTION_MESSAGE);
return 0;
break;
}
else if (num1 > num2)
{
message = JOptionPane.showInputDialog(null, "Sorry too high! Try again", "Guessing Game", JOptionPane.QUESTION_MESSAGE);
return 1;
}
else
{
message = JOptionPane.showInputDialog(null, "Sorry too low! Try again", "Guessing Game", JOptionPane.QUESTION_MESSAGE);
return -1;
}
return num2;
}
}
Can someone please help me with this ?
It seems I am quite confuse with the way how the methods are called.
Thanks in advance!
Okupa