2014-03-24 62 views
-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(); 
    } 

    } 
+0

你能澄清一下,你的代碼出了什麼問題,你在做什麼。另外,你可能想用equals()來比較對象。 – 2014-03-24 20:02:38

回答

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(); 
    } 

    }