Need help with this program please! using arrays You will be writing a number gu
ID: 3551222 • Letter: N
Question
Need help with this program please! using arrays
You will be writing a number guessing game in Java. The program should have 3 difficulty levels, each one
having a different maximum number. After the user selects a difficulty, generate a random number between 1
and X, X being based off of the selected difficulty. The minimum value of the random number should be 1 and
the maximum should be X. Then prompt the user for their guess. If they are wrong, tell them they need to
guess either higher or lower and make them keep guessing until they guess correctly.
The difficulty levels are:
? Easy 1-10
? Normal 1-100
? Hard 1-1000
You may use console input/output or JOptionPane so long as it is consistent and documented
Explanation / Answer
import javax.swing.JOptionPane;
import java.util.*;
public class FunctionTest {
public static void main(String args[]){
int min = 1, x = 10, level, guess, num;
level = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the level(1, 2, 3): "));
if(level == 1) x = 10;
else if(level == 2) x = 100;
else if(level == 3) x = 1000;
Random r = new Random();
num = r.nextInt(x) + 1;
guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the Guess): "));
while(true){
if(guess == num){
JOptionPane.showMessageDialog(null, "Correct guess!! ");
break;
}
else if(guess < num) JOptionPane.showMessageDialog(null, "Try something high, try again: ");
else if(guess > num) JOptionPane.showMessageDialog(null, "Try something low, try again: ");
guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the Guess): "));
}
}
}