Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that plays a guessing game with the user. The program should gen

ID: 3621546 • Letter: W

Question

Write a program that plays a guessing game with the user. The program should generate a random number between 1 and 100, and then prompt the user repeatedly to guess the number. When the user guesses incorrectly, the game should give the user a hint about the number of digits of the player’s guess matches the correct number. Once the user guesses correctly, the program should print a message showing the number of guesses that the user has made.

Sample log of the program execution may be as follows:

Try to guess my two-digit number, and I’ll tell you how many digits from your guess appear in my number.
Your guess? 13
Incorrect (hint: 0 digits match)
Your guess? 26
Incorrect (hint: 0 digits match)
Your guess? 78 Incorrect
(hint: 1 digits match)
Your guess? 70
Incorrect (hint: 2 digits match)
Your guess? 7
You got it right in 5 tries.

Explanation / Answer

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GuessNumber extends JApplet implements ActionListener { JTextField input; JLabel prompt; int answer; public void init() { input = new JTextField( 4 ); input.addActionListener( this ); prompt = new JLabel( "Guess a number between 1 and 100" ); Container container = getContentPane(); container.setLayout( new FlowLayout() ); container.add( prompt ); container.add( input ); answer = getNumber(); } public void actionPerformed( ActionEvent e ) { int userGuess = Integer.parseInt( input.getText() ); checkUserGuess( userGuess ); input.setText( "" ); } public int getNumber() { return ( ( int ) ( 1 + Math.random() * 100 ) ); } public void checkUserGuess( int userGuess ) { if ( userGuess answer ) showStatus( userGuess + " is too high. Try again." ); else { showStatus( "Congratulations. You guessed the number!" ); input.setText( "" ); answer = getNumber(); } } } I hope this will helps to You !
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote