-1
試圖創建一個帶循環功能的高低遊戲來計算嘗試的數量,並允許用戶保持重新輸入,直到生成的隨機數與用戶輸入的值相匹配,獲得輸出消息,如果它太低或太高,但不循環功能或清除該字段以允許另一種猜測循環功能清除字段
這是我到目前爲止;
/**
* A very simple guessing game program.
*
* The program thinks of a random number between 1 and 100
* and then asks the user to guess the number. An
* appropriate message is displayed depending upon a right
* or wrong answer.
*
*/
import javax.swing.*; // JPanel, JLabel, etc
import java.awt.event.*; // ActionListener, ActionEvent
import java.awt.*; // Color
import java.util.*; // Random Class
class HiLo8 implements ActionListener
{
// Define object references
JFrame myFrame;
JPanel myPanel;
JButton startButton, makeGuessButton, submitButton,
playAgainButton, exitButton;
JLabel message1Label, message2Label, message3Label;
JTextField inputNumberTxtField;
Random randomGenerator;
int randomNumber;
// Constructor method
HiLo8()
{
// Set up frame
myFrame = new JFrame("Guess a number game");
// Set up panel object. Use absolute positioning.
myPanel = new JPanel();
myPanel.setLayout (null);
// Create a random generator object
randomGenerator = new Random();
// This push button is used to start the game off.
startButton = new JButton ("Press to start game...");
startButton.setBounds (10, 30, 400, 30);
myPanel.add (startButton);
// This label object (initially with no string)
// will be used to let the user know that the
// program is thinking of a number
message1Label = new JLabel();
message1Label.setBounds (10,80,500,20);
myPanel.add (message1Label);
makeGuessButton = new JButton ("Click to make your guess.. ");
makeGuessButton.setBounds (10, 140, 400, 30);
myPanel.add (makeGuessButton);
// This label object will be used to prompt the
// user to enter a number
message2Label = new JLabel ("What is the number? ");
message2Label.setBounds (10, 200, 500, 20);
myPanel.add (message2Label);
// The textfield object where the user types in
// a number. A default value of 1 is provided
inputNumberTxtField = new JTextField ("1");
inputNumberTxtField.setBounds (220, 200, 30, 20);
myPanel.add (inputNumberTxtField);
// After the user types in a number (guess),
// he/she must then press this button to continue
submitButton = new JButton ("Submit your guess");
submitButton.setBounds (300, 200, 150, 30);
myPanel.add (submitButton);
// This object (initially empty) will let
// the user know whether the guess or right
// or wrong
message3Label = new JLabel();
message3Label.setBounds (10, 250, 500, 20);
myPanel.add (message3Label);
// This push button allows the user to
// exit the application.
exitButton = new JButton ("Exit Game");
exitButton.setBounds (250, 300, 100, 30);
myPanel.add (exitButton);
// The following components are initially made
// invisible
message2Label.setVisible (false);
makeGuessButton.setVisible (false);
inputNumberTxtField.setVisible (false);
submitButton.setVisible (false);
exitButton.setVisible (false);
// Register all push button objects
// for an ActionEvent
startButton.addActionListener (this);
makeGuessButton.addActionListener (this);
submitButton.addActionListener (this);
exitButton.addActionListener (this);
// Add panel to frame
myFrame.add (myPanel);
// Size frame and make it visible
myFrame.setBounds(50,100,500,450);
myFrame.setVisible(true);
}
// Implement the actionPerformed method
public void actionPerformed(ActionEvent event)
{
// Local variables
String str;
int number;
int numberOfTries = 0;
// Did the user press the Start button
if (event.getSource() == startButton)
{
// Disable the startButton for now
startButton.setEnabled (false);
// Tell user the program is thinking of a
// number
message1Label.setText ("Thinking of a number between 1 - 100");
// Generate a random number between 1 to 100
// nextInt (100) generates a random no.
// between 0 to 9, so add 1 on to scale up
randomNumber = randomGenerator.nextInt(100) + 1;
numberOfTries++;
// Make the makeGuess button visible
makeGuessButton.setVisible (true);
}
else // Check to see if it's the makeGuessButton
if (event.getSource() == makeGuessButton)
{
// Disable makeGuessButton for now
makeGuessButton.setEnabled (false);
// All the elements asking the user to
// make a guess and submit answer are now
// made visible
message2Label.setVisible (true);
inputNumberTxtField.setVisible (true);
submitButton.setVisible (true);
// Make sure the cursor is in the textfield
inputNumberTxtField.requestFocus();
}
else // Check to see if it's the submit button
if (event.getSource() == submitButton)
{
// Read the contents off the textfield
str = inputNumberTxtField.getText();
// Convert to an integer number
number = Integer.valueOf (str);
// Disable textfield and submitbutton
inputNumberTxtField.setEnabled (false);
submitButton.setEnabled (false);
// Check to see if user guessed correctly
if (number == randomNumber)
{ // Guessed correctly
message3Label.setText ("Well done. You've guessed correctly" + " it's taken " + numberOfTries + " tries");
}
else if (number < randomNumber) // Otherwise, got it wrong!
{
message3Label.setText ("Your guess is too low" + " it's taken " + numberOfTries + " tries");
}
else if (number > randomNumber) // Otherwise, got it wrong!
{
message3Label.setText ("Your guess is too high" + " it's taken " + numberOfTries + " tries");
}
// Make the exitButton
// visible
exitButton.setVisible (true);
}
else // exitButton pressed
if (event.getSource() == exitButton)
{
System.exit (0); // Exit the application
}
}
// main() method - this is where program class execution
// starts
public static void main()
{
HiLo8 start = new HiLo8();
}
}
你能澄清一下,你的代碼出了什麼問題,你在做什麼。另外,你可能想用equals()來比較對象。 – 2014-03-24 20:02:38