我试图添加一个while循环来计算尝试次数,并将其添加到GUI中的消息中。添加while循环后,程序将冻结,但不会向控制台输出错误。
我已经在没有GUI的情况下对其进行了测试,并且可以使用System.out.print很好地进行打印,但是我什至无法允许第二次输入。我必须停止运行代码才能退出。
任何建议将不胜感激!
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.*;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuessingGame extends JFrame {
private JTextField txtGuess;
private JLabel lblOutput;
private int theNumber;
private int attempt;
public void checkGuess() {
String guessText = txtGuess.getText();
String message = "";
try {
int guess = Integer.parseInt(guessText);
while(guess!=theNumber) {
if (guess < 0 || guess >100) {
message = "Please enter a number between 0 and 100";
attempt++;
}
else if (guess < theNumber) {
message = guess + " is too low. Try again.";
attempt++;
}
else if (guess > theNumber) {
message = guess + " is too high. Try again.";
attempt++;
}
else {
message = guess + " is correct! It took " + attempt+ " attempts. Starting new game.";
attempt++;
newGame();
}
}
}
catch(Exception e) {
message= "Enter a whole number between 0 and 100";
}
finally {
lblOutput.setText(message);
txtGuess.requestFocus();
txtGuess.selectAll();
}
}
public void newGame() {
theNumber = (int)(Math.random() * 100 + 1);
}
public GuessingGame() {
setTitle("Hi-Lo Guessing Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
JLabel lblHiloGuessingGame = new JLabel("Hi-Lo Guessing Game");
lblHiloGuessingGame.setFont(new Font("Tahoma", Font.BOLD, 15));
lblHiloGuessingGame.setHorizontalAlignment(SwingConstants.CENTER);
lblHiloGuessingGame.setBounds(12, 35, 408, 16);
getContentPane().add(lblHiloGuessingGame);
JLabel lblGuessANumber = new JLabel("Guess a number between 1 and 100");
lblGuessANumber.setHorizontalAlignment(SwingConstants.CENTER);
lblGuessANumber.setBounds(58, 86, 245, 16);
getContentPane().add(lblGuessANumber);
txtGuess = new JTextField();
txtGuess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkGuess();
}
});
txtGuess.setBounds(310, 85, 32, 19);
getContentPane().add(txtGuess);
txtGuess.setColumns(10);
JButton btnGuess = new JButton("Guess!");
btnGuess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkGuess();
}
});
btnGuess.setBounds(163, 137, 97, 25);
getContentPane().add(btnGuess);
lblOutput = new JLabel("Enter a number and click guess");
lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
lblOutput.setFont(new Font("Tahoma", Font.ITALIC, 13));
lblOutput.setBounds(84, 197, 265, 19);
getContentPane().add(lblOutput);
}
public static void main(String[] args) {
GuessingGame theGame = new GuessingGame();
theGame.newGame();
theGame.setSize(new Dimension(450,300));
theGame.setVisible(true);
}
}
答案 0 :(得分:1)
我认为您不需要为文本字段执行由操作执行的处理程序,因为对文本字段执行任何操作都不应算作尝试,对吧?仅按Guess按钮才算是尝试。
因此您可以删除它:
txtGuess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkGuess();
}
});
现在,只要用户按下按钮,就会调用checkGuess
。现在,它将解析文本字段中的数字,并进入while循环以检查该数字。但是,由于guess
从未在while循环中被修改,因此while循环永不中断。
即使您在while循环中读取了guess
,guess
仍然不会改变,因为在while循环运行时,用户无法与进行交互用户界面。
一种更好的方法是简单地删除while循环:
public void checkGuess() {
String guessText = txtGuess.getText();
String message = "";
try {
int guess = Integer.parseInt(guessText);
if (guess < 0 || guess >100) {
message = "Please enter a number between 0 and 100";
attempt++;
}
else if (guess < theNumber) {
message = guess + " is too low. Try again.";
attempt++;
}
else if (guess > theNumber) {
message = guess + " is too high. Try again.";
attempt++;
}
else {
message = guess + " is correct! It took " + attempt+ " attempts. Starting new game.";
attempt++;
newGame();
}
}
catch(Exception e) {
message= "Enter a whole number between 0 and 100";
}
finally {
lblOutput.setText(message);
txtGuess.requestFocus();
txtGuess.selectAll();
}
}
尽管似乎您需要在此处进行while循环,但实际上并不需要。这是因为每次您按下Guess按钮时都会调用checkGuess
。
与在命令行中不同,您不询问在GUI程序中输入,而是等待。