|
#1
|
||||
|
||||
![]()
<div><div align="center"><b><font color="Blue"><font size="3">السلام عليكم
السؤال Write a Java application that plays “guess the number” as follows: your program chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The application displays the prompt Guess a number between 1 and 1000. The player inputs a first guess. If the player’s guess is correct, display Congratulations, you have guessed the number. If the player’s guess is incorrect, your program should display Too high, guess again, or Too low, guess again. The program should prompt the user for the next guess until the correct answer is given, then the screen displays Congratulations, you have guessed the number, and allow the user to choose whether to play again الي فوق المطلوب ولكن هناك مشكلة في الكود وهناك بعض الاشياء مضافة على الكود اريد تعديل الكود ليتوافق مع السؤال في الاعلى import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.Random; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JButton; public class GuessGameFrame extends JFrame { private static Random generator = new Random(); private int number; // number chosen by application private int guessCount; // number of guesses private int lastDistance; // distance between last guess and number private JTextField guessInputJTextField; // for guessing private JLabel prompt1JLabel; // first prompt to user private JLabel prompt2JLabel; // second prompt to user private JLabel messageJLabel; // displays message of game status private JButton newGameJButton; // creates new game private Color background; // background color of application // set up GUI and initialize values public GuessGameFrame() { super( "Guessing Game" ); /* Write a line of code that calls the superclass constructor and sets the title of this application to "Guessing Game" */ guessCount = 0; // initialize number of guesses to 0 background = Color.LIGHT_GRAY; // set background to light gray prompt1JLabel = new JLabel( "I have a number between 1 and 1000." ); // describe game prompt2JLabel = new JLabel( "Can you guess my number? Enter your Guess:" ); // prompt user guessInputJTextField = new JTextField( 5 ); // to enter guesses guessInputJTextField.addActionListener( new GuessHandler( ) ); messageJLabel = new JLabel( "Guess result appears here." ); newGameJButton = new JButton( "New Game" ); // button with text add ( newGameJButton ); // add newGame button to JFrame /* Write a statement that creaters the "New Game" button */ // create new ButtonHandler for button event handling newGameJButton.addActionListener( new ActionListener() // anonymous inner class { public void actionPerformed( ActionEvent e ) { /* Write code that resets the application to an appropriate state to start a new game. Reset the background color to light gray, set the JTextFields to their initial text, call method theGame and repaint the GuessGame JFrame */ } // end method actionPerformed } // end anonymous inner class ); // end call to addActionListener /* Write code that will set the layout of the container to a Flowlayout, then add all the GUI components to the container */ theGame(); // start new game } // end GuessGameFrame constructor // choose a new random number public void theGame() { number = ( int ) ( Math.random() * 1000 + 1 ); /* Write a statement that sets instance variable number to a random number between 1 and 1000 */ } // end method theGame // change background color public void paint( Graphics g ) { super.paint( g ); getContentPane().setBackground( background ); // set background } // end method paint // react to new guess public void react( int guess ) { guessCount++; // increment guesses /* Write code that sets instance variable currentDistance to 1000. This variableís value will be used to determine if th ebackground color should be set to red or blue to indicate that the last guess was getting closer to or further from the actual number. */ // first guess if ( guessCount == 1 ) { /* Write code to set instance variable lastDistance to the absolute value of the difference between variables guess and number. This value will be used with subsequent guesses to help set the background color. */ if ( guess > number ) messageJLabel.setText( "Too High. Try a lower number." ); else messageJLabel.setText( "Too Low. Try a higher number." ); } // end if else { /* Write code that sets instance variable currentDistance to the absolute value of the difference between variables guess and number. This variableís value will be compared with lastDistance to determine the background color. */ // guess is too high if ( guess > number ) { messageJLabel.setText( "Too High. Try a lower number." ); /* Write code that sets Color variable background to red if the currentDistance is less than or equal to lastDistance; otherwise, set background to blue. Then assign currentDistance to lastDistance. */ } // end if else if ( guess < number ) // guess is too low { messageJLabel.setText( "Too Low. Try a higher number." ); background = ( currentDistance |
مواقع النشر (المفضلة) |
|
|
![]() |
||||
الموضوع | كاتب الموضوع | المنتدى | مشاركات | آخر مشاركة |
طلب شرح برنامج rt seven lite لتعديل على ويندوز سفن | RSS | Arabic Rss | 0 | 01-19-2011 01:04 PM |
نتنياهو يعد لتعديل وزاري | RSS | المجاوشي للأخبار العامه والسياسية والرياضية | 0 | 01-18-2011 05:54 PM |
مصر تتجه لتعديل ديني على المناهج | RSS | المجاوشي للأخبار العامه والسياسية والرياضية | 0 | 04-26-2010 09:50 PM |
البرادعي عضو بجمعية لتعديل الدستور | RSS | المجاوشي للأخبار العامه والسياسية والرياضية | 0 | 02-24-2010 07:52 PM |
رفض بالكويت لتعديل قانون الإعلام | المجاوشي | المجاوشي للأخبار العامه والسياسية والرياضية | 0 | 01-29-2010 11:38 PM |
|