尝试使用循环函数创建一个高低游戏来计算尝试次数并允许用户继续重新输入,直到生成的随机数与用户输入的值匹配,获取输出消息,如果它&# 39; s太低或太高但没有循环功能或清除字段以允许另一个猜测
这是我到目前为止所拥有的;
/**
* 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();
}
}
答案 0 :(得分:0)
我编辑了一些代码,但在/ * * /中留下了注释,因此您可以将它们与您自己的区别开来。如果您希望代码适合您,请检查它。当您使用JFrame setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE)创建应用程序时,友好的建议是您的朋友。
/**
* 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
{
// Define object references
JFrame myFrame;
JPanel myPanel;
JButton startButton, makeGuessButton, submitButton,
playAgainButton, exitButton;
JLabel message1Label, message2Label, message3Label;
JTextField inputNumberTxtField;
Random randomGenerator;
int randomNumber;
/*
* variables moved from local to global
*/
String str;
int number;
int numberOfTries = 0;
// 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
/*
* actionListener for startButton
*/
startButton.addActionListener (new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 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;
/*
* i removed this since logically you should only be increasing 'tries' when the submitButton is pushed
*/
//numberOfTries++;
// Make the makeGuess button visible
makeGuessButton.setVisible ( true );
}
});
/*
* actionListerner for makeGuessButton
*/
makeGuessButton.addActionListener (new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 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();
}
});
/*
* action listener for submitButton
*/
submitButton.addActionListener (new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Read the contents off the textfield
str = inputNumberTxtField.getText();
// Convert to an integer number
number = Integer.valueOf ( str );
/*
* increase number of 'tries'
*/
numberOfTries++;
// Disable textfield and submitbutton
/* inputNumberTxtField.setEnabled ( false );
* submitButton.setEnabled ( false );
* keep the textField and button enabled until the user guesses properly
* otherwise the user wont be able to make another guess
*/
// 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");
/*
* disable the textField and button
*/
inputNumberTxtField.setEnabled(false);
submitButton.setEnabled(false);
}
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 );
}
});
/*
* actionListener for exitButton
*/
exitButton.addActionListener (new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// Add panel to frame
myFrame.add (myPanel);
// Size frame and make it visible
myFrame.setBounds(50,100,500,450);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
}
// main() method - this is where program class execution
// starts
public static void main(String[] args)
{
HiLo8 start = new HiLo8();
}
}