Java I need to modify this code so that I am using a dialogue box instead of sca
ID: 3940658 • Letter: J
Question
Java I need to modify this code so that I am using a dialogue box instead of scanner and I have no idea where to start. I am supposed to use showInputDialog() and showMessageDialog() to get the input and produce the output of GuessANumber. This is what I have so far.
import java.util.Random;
import java.util.Scanner;
public class GuessANumber //First, name the new class, and .java file, GuessANumber_yourinitials.
{
public static void main (String[] args)
{
//Then, create an object of the Random class and call the nextInt() method to generate random number from 1 - 1000.
//Be careful to generate the correct range - not 0 to 999. Now you have the number that the player has to guess.
Random rand = new Random();
int numberToGuess = rand.nextInt(1000);
int numberOfTries = 0; //Add an int variable to keep up with the number of guesses, and start it off at 0.
int guess;
Scanner input = new Scanner(System.in);
boolean win = false;
while (win == false) //loop until correct answer is guessed
{
System.out.println("Guess a number between 1 and 1000: "); //Add a statement to prompt the user to enter a guess between 1 and 1000.
guess = input.nextInt();//Now, user Scanner and nextInt() to read the guess.
numberOfTries++; //Update the count on the number of guesses
if (guess == numberToGuess) //User conditionals to let the user know if the guess is too low, too high or CORRECT!
{
win = true;
}
else if (guess< numberToGuess)
{
System.out.println();
System.out.println ("The number you guessed is too low please ");
}
else if (guess > numberToGuess)
{
System.out.println();
System.out.println ("The number you guessed is too high please ");
}
}
System.out.println("You win!");
System.out.println("The number was " + numberToGuess); //output the correct answer and the number of tries
System.out.println("It took you " + numberOfTries + " Tries");
}
}
Explanation / Answer
Hi,
I have modified your code. it is working fine as expected. Highlighted the code changes below.
GuessANumber.java
import java.util.Random;
import javax.swing.JOptionPane;
public class GuessANumber //First, name the new class, and .java file, GuessANumber_yourinitials.
{
public static void main (String[] args)
{
//Then, create an object of the Random class and call the nextInt() method to generate random number from 1 - 1000.
//Be careful to generate the correct range - not 0 to 999. Now you have the number that the player has to guess.
Random rand = new Random();
int numberToGuess = rand.nextInt(1000);
int numberOfTries = 0; //Add an int variable to keep up with the number of guesses, and start it off at 0.
int guess;
// Scanner input = new Scanner(System.in);
boolean win = false;
while (win == false) //loop until correct answer is guessed
{
//System.out.println("Guess a number between 1 and 1000: "); //Add a statement to prompt the user to enter a guess between 1 and 1000.
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number between 1 and 1000: "));
numberOfTries++; //Update the count on the number of guesses
if (guess == numberToGuess) //User conditionals to let the user know if the guess is too low, too high or CORRECT!
{
win = true;
}
else if (guess< numberToGuess)
{
// System.out.println();
JOptionPane.showMessageDialog(null, "The number you guessed is too low please ");
//System.out.println ("The number you guessed is too low please ");
}
else if (guess > numberToGuess)
{
// System.out.println();
JOptionPane.showMessageDialog(null, "The number you guessed is too high please ");
//System.out.println ("The number you guessed is too high please ");
}
}
JOptionPane.showMessageDialog(null, "You win! The number was " + numberToGuess+" It took you " + numberOfTries + " Tries");
// System.out.println("You win!");
// System.out.println("The number was " + numberToGuess); //output the correct answer and the number of tries
// System.out.println("It took you " + numberOfTries + " Tries");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.