2015-11-25 50 views
0

我在猜數字程序,我在循環中遇到問題。當我運行程序並在文本框中輸入一個數字,然後按回車就會凍結。我發現這可能是由於無限循環而發生的。如果我錯了,隨時糾正我。基本上,當我在文本框中輸入一個數字並按下輸入時,它假設改變一個標籤並更改背景顏色,但這不會發生,我認爲它是因爲我的循環運行,直到win變成true,當我鍵入我的號碼時,它一直運行這個數字而不是輸出正確的標籤,讓我輸入一個不同的數字到文本字段中。 P.S:我知道newGame按鈕不起作用猜數字遊戲的圖形用戶界面 - 無限循環

import java.util.*; 
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class GuessingGame implements ActionListener 
{ 
    JFrame guessFrame; 

    JPanel guessPanel; 

    JTextField guessText; 

    JButton newGame; 

    JLabel rangeLbl, enterGuessLbl, winLbl; 

    Random rand = new Random(); 
    int numToGuess = rand.nextInt(1000)+1; 
    int numOfTries = 0; 
    int guess; 

    public GuessingGame() 
    { 
     // Create the frame and container. 
     guessFrame = new JFrame("Guess the Number"); 
     guessPanel = new JPanel(); 
     guessPanel.setLayout(new GridLayout(5,0)); 


     // Add the widgets. 
     addWidgets(); 

     // Add the panel to the frame. 
     guessFrame.getContentPane().add(guessPanel, BorderLayout.CENTER); 

     // Exit when the window is closed. 
     guessFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Show the converter. 
     guessFrame.pack(); 
     guessFrame.setVisible(true); 
    } 

    // Create and add the widgets for converter. 
    private void addWidgets() 
    { 
     // Create widgets. 
     guessText = new JTextField(); 
     guessText.setHorizontalAlignment(JTextField.CENTER); 
     rangeLbl = new JLabel("I have a number between 1 and 1000. Can you guess my number?", SwingConstants.LEFT); 
     enterGuessLbl = new JLabel("Please enter your guess", SwingConstants.LEFT); 
     winLbl = new JLabel(" ", SwingConstants.CENTER); 
     newGame = new JButton("New Game"); 

     // Listen to events from Convert textfield. 
     guessText.addActionListener(this); 

     // Add widgets to container. 
     guessPanel.add(rangeLbl); 
     guessPanel.add(enterGuessLbl); 
     guessPanel.add(guessText); 
     guessPanel.add(winLbl); 
     guessPanel.add(newGame); 
    } 

    // Implementation of ActionListener interface. 
    public void actionPerformed(ActionEvent event) 
    {   
     boolean win = false; 

     guess = Integer.parseInt(guessText.getText()); 

     if (guess == numToGuess) 
     { 
      win = true; 
     } 
     else if (guess < numToGuess) 
     { 
      winLbl.setText("Too Low"); 
      guessPanel.setBackground(Color.red); 
      guess = Integer.parseInt(guessText.getText()); 
     } 
     else if (guess > numToGuess) 
     { 
      winLbl.setText("Too High"); 
      guessPanel.setBackground(Color.blue); 
      guess = Integer.parseInt(guessText.getText()); 
     } 

     winLbl.setText("Correct!"); 
     guessPanel.setBackground(Color.green); 
    } 

    public static void main(String[] args) 
    { 
     GuessingGame game = new GuessingGame(); 
    } 

} 

回答

0

您的while循環在這裏不合適,因爲您處於actionPerformed()方法。這種方法最有可能在gui動作中被調用(例如按鈕被踢)。

它應該根據您的需要做一個動作,然後終止,因爲這個方法在EDT中被調用。此方法完成後,您的gui將不會執行任何更新。

因此,在用戶做出一些額外的動作之前,沒有什麼會改變(例如,您的獲勝狀態),因爲您的GUI被凍結,所以他不能這樣做。

+0

我認爲我是理解也許我只是編輯了問題發佈完整的代碼 – TubaShark

+0

刪除while循環建議,你不需要它在這裏。 – Marcinek

+0

我刪除了while循環,現在我輸入的任何數字都是正確的哈哈 – TubaShark

0

您不更新猜測的值。您需要在循環結束時讀取猜測,或者至少在猜測和numToGuess不具有相同值的情況下。

在當前的情況下:如果值不等於第一次迭代,它將永遠不會變成。

添加

guess = Integer.parseInt(guessText.getText()); 

去年語句每個否則,如果塊

編輯:一種更好的方式,因爲Marcinek指出,將刪除while循環,但因爲我不知道你的要求,我不會去宣稱它是正確的溶劑。

+0

我想我理解也許我只是編輯的問題發佈完整的代碼 – TubaShark

+0

如果您需要用戶繼續輸入號碼,直到他得到了它的權利,這將做到這一點確實如此。但是,如果他獲得了正確的價值,你也會問一個新的價值。這不是必需的。但是你需要在那裏迭代,還是隻檢查當前值? – Stultuske

+0

如果他得到了正確的隨機數,我想讓textfield不可編輯並顯示它是正確的。然後開始一個新遊戲,你將不得不按下按鈕。用戶需要繼續向文本字段輸入數字,直到猜測正確爲止 – TubaShark

0

你的循環保持乳寧,因爲這種情況if (guess == numToGuess)從未verfied

  boolean win = false; 

      while (win == false){ 

     if (guess == numToGuess){ 
      win = true; 
      } 
      ....... 
      } 

和 取勝還是假的,while循環去頭乳寧。

while (win == false) 
{ 
    .....}