Question: I need help with this java programming homework assignment. I had to take the code for a program that already works, and just add a

I need help with this java programming homework assignment.

I had to take the code for a program that already works, and just add a GUI to it. The program is supposed to be a number guessing game, where the computer guesses a number, and you have to type in what number you think the program is thinking of. It is then supposed to tell you if your guess is too high, too low, or correct.

My program technically runs, but I have an issue. It's with the prompt box that comes up asking you to type in a number. When I type a number into the text box, the program just closes completely. It doesnt tell me if my number I guessed is close to what the computer guessed, or if it is far off, or if the guess was correct.

Here is the code for my program.

import javax.swing.*; import java.util.Random; import java.util.Scanner;

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; Scanner inputStream = null;

// Loop until the user has guessed the number while (guess!=number) { // Prompt the user for their next guess String InputText = JOptionPane.showInputDialog("Enter your guess: "); // Read the users guess guess = inputStream.nextInt(); // Check if the guess is high, low or correct if (guess < number) { // Guess is too low JOptionPane.showMessageDialog( null,"too low"); } else if (guess>number) { // Guess is too high JOptionPane.showMessageDialog(null,"Too high, please try again"); } else { // Guess is correct !! JOptionPane.showMessageDialog(null,"Correct, the number was " + number); } } } }

Hopefully someone can help me.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!