germaap.blogg.se

Guessing game in java with source code
Guessing game in java with source code













  1. Guessing game in java with source code how to#
  2. Guessing game in java with source code code#

Ok, we need the computer to generate random numbers. If you're using Eclipse (and I strongly urge you to!) then make sure to checkmark the box to have it put in your main method for you. Call your new program GuessingGame, keeping the capitalization the same. Ready to follow along to create this guessing game? All right.įirst, we're going to start by creating a new class, or Java file. At the end of the game, the number is revealed along with the number of guesses it took to get the correct number. For every guess, the computer will either say "Too high" or "Too low", and then ask for another input. The player must then continue to guess numbers until the player guesses the correct number. Thank you so much!!! It works great now! I appreciate it so much.In this guessing game, the computer will come up with a random number between. Hope your game rocks!:) // choose a new random number The second problem/request (clearing the text field after every guess) is a matter of repeating the clearing occuring every time a new game is created (the first line of the "theGame" method) every time a new guess has been made. The first problem you mention (the first guess being colder) was created by the GuessOld variable not being reinitialized every time the game got started again.

Guessing game in java with source code code#

They are only two and are marked with the red in the code below. The changes you need to make are in the "theGame()" method. tDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) Public static void main(String args) throws Exception SwingUtilities.updateComponentTreeUI(message3JLabel) tText("# of guesses = " + Integer.toString(Guesses)) GetContentPane().setBackground(Color.YELLOW) tText("Congratulations! You guessed my number!") tText("Enter guess # " + Integer.toString(Guesses)) SwingUtilities.updateComponentTreeUI(messageJLabel) GetContentPane().setBackground(Color.BLUE) SwingUtilities.updateComponentTreeUI(message2JLabel) If (Math.abs(number - Guess) Math.abs(number - GuessOld)) SwingUtilities.updateComponentTreeUI(guessInputJTextField) Guess = Integer.parseInt(guessInputJTextField.getText()) GetContentPane().setBackground(Color.LIGHT_GRAY) Ĭlass GuessHandler implements ActionListener tText("Enter guess # " + Integer.toString(++Guesses)) SwingUtilities.updateComponentTreeUI(random1) Public void actionPerformed(ActionEvent e) NewGameJButton.addActionListener(new ActionListener() // anonymous inner class Number = generator.nextInt(1001) //create random number

guessing game in java with source code

NewGameJButton = new JButton("New Game") // create "New Game" buttonĪdd(newGameJButton) // add newGame button to JFrame GuessInputJTextField.addActionListener(new GuessHandler()) GuessInputJTextField = new JTextField(5) // to enter guesses Prompt2JLabel = new JLabel("Can you guess my number? Type your Guess and then press 'Enter':") // prompt user Prompt1JLabel = new JLabel("I have a number between. Private Color background // background color of applicationīackground = Color.LIGHT_GRAY // set background to light gray Private JButton newGameJButton // creates new game Private JLabel message2JLabel //display number of guesses Private JLabel messageJLabel // displays message of game status Private JLabel prompt2JLabel // second line of instructions Private JLabel prompt1JLabel // first line of instruction Private JTextField guessInputJTextField // user input field Private int number // application's number

Guessing game in java with source code how to#

Can anyone give me some advice here on how to fix these two things? Here's my code: import I feel like I have tried everything but I know I must be missing something. The other thing I want to change is, after the user enters a guess, I want the the JTextField to clear itself so that for the next guess, the user doesn't have to backspace to enter a new guess. Well, it usually turns red after the first guess but sometimes it turns blue after the first guess and I can't figure out how to make sure it turns red after the first guess every time.

guessing game in java with source code guessing game in java with source code

After the very first guess, the background should turn red with a message saying that you're getting warmer and any other time you're getting warmer or colder (blue background for colder). So anyway, this program generates a random number and the user tries to guess it. I had posted my code and updated it a few times in another thread and didn't get any response and since I have gotten some things figured out and had posted my long code a few times in the other thread, I thought I would put my last couple questions in a new thread.















Guessing game in java with source code