Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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"); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions

Question

Prepare an ID card of the continent Antarctica?

Answered: 1 week ago

Question

LO5.2 Discuss government failure and explain why it happens.

Answered: 1 week ago