This JAVA code prompts the user to guess a number between 1 and 1000. If the use
ID: 672438 • Letter: T
Question
This JAVA code prompts the user to guess a number between 1 and 1000. If the user's guess is too high or two low, the program tells them, and poses the question again. The user gets infinite guesses. Once the user gets it correct, the program tells them and the game ends.
WHAT I NEED:
1) I need all of the prompts and user entry boxes to pop-up in windows, preferrably using JOptionPane
2) Once the user guesses the correct number, instead of it simply ending, I need the program to ask the user if they would like to play again, giving the options "Y" (for yes, after which it will restart and ask the question again with a new number), and "N" (for no, after which the program terminates). This needs to be in a pop-up as well.
--------------------------------------------------------------------------------------------------------------------------------------------
MY CODE THUS FAR:
package GuessingGame;
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = rand.nextInt(1000);
int numberOfTries = 0;
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;
while (win == false) {
System.out.println("Guess a number between 1 and 1,000: ");
guess = input.nextInt();
numberOfTries++;
if (guess == numberToGuess) {
win = true;
}
else if (guess < numberToGuess) {
System.out.println("Too low, try again");
}
else if (guess > numberToGuess) {
System.out.println("Too high, try again");
}
}
System.out.println("Congratulations you guessed the number");
System.out.println("The number is" + numberToGuess);
System.out.println("It took you " + numberOfTries + " tries ");
}
}
Explanation / Answer
package mani;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class GuessingGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = 0;
int numberOfTries = 0;
Scanner input = new Scanner(System.in);
String t="Y";
int guess;
boolean win = false;
do{
numberToGuess = rand.nextInt(1000);
numberOfTries = 0;
System.out.println(numberToGuess);
win=false;
while (win == false) {
System.out.println("Guess a number between 1 and 1,000: ");
guess = Integer.parseInt(JOptionPane.showInputDialog(null,"Guess a number between 1 and 1,000:"));
numberOfTries++;
if (guess == numberToGuess) {
JOptionPane.showMessageDialog(null, "Yeah!!! You guessed it right!!!!!");
win = true;
}
else if (guess < numberToGuess) {
JOptionPane.showMessageDialog(null, "Too low, try again");
}
else if (guess > numberToGuess) {
JOptionPane.showMessageDialog(null, "Too high, try again");
}
}
JOptionPane.showMessageDialog(null,"Congratulations you guessed the number");
JOptionPane.showMessageDialog(null, "The number is" + numberToGuess);
JOptionPane.showMessageDialog(null, "It took you " + numberOfTries + " tries ");
t = (JOptionPane.showInputDialog(null,"Enter 'Y' to continue or any other to exit!!!"));
}while(t.equalsIgnoreCase("Y"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.