I need help with this java programming project. I had to take an already working
ID: 3826368 • Letter: I
Question
I need help with this java programming project. I had to take an already working program for a guessing game, in which the program thinks of a number from 0-99, and I have to guess the number the computer is thinking of, and just simply add a gui to it. Each time i type in a guess, the program is supposed to tell me if my guess is too high, too low, or correct. I had submitted it to my professor already, and he messaged me telling me there's a bug with my program, which he will let me fix.
Basically my professor said the number the program chooses is transitory. It does not remain the same. and as a result, my algorithim for the program does weird things. Like if i change the random number generator to a max of 10, and i enter in the number 11 as a guess when I run the program , it says my guess is too low. I am hoping someone can help me, because I have no clue how to fix it.
my code for the program
import javax.swing.*;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class HighLowGuessingGameGUI {
public static void main(String[] args) {
// Create a random number generator
Random random = new Random();
// Use Scanner for getting input from user
Scanner scanner = new Scanner(System.in);
// Use the random generator to
// pick a number between 0 and 99 (inclusive)
int number = random.nextInt(100);
int guess = -1;
JFrame frame = new JFrame("InputDialog");
guess = Integer.valueOf(JOptionPane.showInputDialog(frame, "Enter your guess:"));
if (guess != number) {
if (guess < number) {
JOptionPane.showMessageDialog(null,guess, "too low", JOptionPane.PLAIN_MESSAGE);
}
else if (guess > number) {
JOptionPane.showMessageDialog(null,guess, "Too high, please try again", JOptionPane.PLAIN_MESSAGE); }
else {
JOptionPane.showMessageDialog(null,guess, "Correct, the number was " + number,JOptionPane.PLAIN_MESSAGE); }
}
else{
System.out.println("bye");
}
}
}
Explanation / Answer
//Modified Program is given below
//We print random number to console to check our program is working right or not.
import javax.swing.*;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class HighLowGuessingGameGUI{
public static void main(String[] args) {
// Create a random number generator
Random random = new Random();
// Use Scanner for getting input from user
Scanner scanner = new Scanner(System.in);
// Use the random generator to
// pick a number between 0 and 99 (inclusive)
int number = random.nextInt(100);
int guess = -1;
JFrame frame = new JFrame("InputDialog");
System.out.println("Random number generated is: "+number);
guess = Integer.valueOf(JOptionPane.showInputDialog(frame, "Enter your guess:"));
if (guess != number) {
if (guess < number) {
JOptionPane.showMessageDialog(null,guess, "too low", JOptionPane.PLAIN_MESSAGE);
}
else if (guess > number) {
JOptionPane.showMessageDialog(null,guess, "Too high, please try again", JOptionPane.PLAIN_MESSAGE); }
}
if(guess==number) {
JOptionPane.showMessageDialog(null,guess, "Correct, the number was " + number,JOptionPane.PLAIN_MESSAGE);
}
System.out.println("bye");
}
}