JOptionPane 中的数字猜谜游戏,JPanel 未显示(Java)
Number guessing game in JOptionPane, JPanel is not showing up(Java)
所以我使用 JOptionPane 制作了一个猜数游戏程序,我的问题是当我 运行 程序没有显示 window即使我构建它时没有错误,程序也会结束。
到目前为止我的代码是这样的。
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class TestNo4 {
public static void main(String[] args) {
Random ranNum = new Random();
int secretNum = ranNum.nextInt(100) + 1;
int guess = 0;
int guessNo = 0;
int guessLimit = 5;
JTextField numUserIn = new JTextField(5);
JPanel getInput = new JPanel();
getInput.add(new JLabel("Enter you guess(Limit of 5 guesses)"));
getInput.add(numUserIn);
for (int countGuess = 0; countGuess == guessLimit; countGuess++) {
int numInput = JOptionPane.showConfirmDialog(null, getInput,
"Guess number " + countGuess, JOptionPane.OK_CANCEL_OPTION);
if (numInput == JOptionPane.OK_OPTION) {
guess = Integer.parseInt(numUserIn.getText());
if (guess == secretNum) {
JOptionPane.showMessageDialog(null, "Congratulations!!",
"Got it in " + countGuess + " tries",
JOptionPane.INFORMATION_MESSAGE);
break;
}
if (countGuess == guessLimit) {
JOptionPane.showMessageDialog(null,
"Sorry, No more guesses left.", "The number is"
+ secretNum,
JOptionPane.INFORMATION_MESSAGE);
break;
}
if (guess > secretNum) {
JOptionPane.showMessageDialog(null, "Hint",
"Try something lower.",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Hint",
"Try something higher.",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
有什么地方出错了吗?
更改以下行即可
for (int countGuess = 0; countGuess == guessLimit; countGuess++) {
和
for (int countGuess = 0; countGuess <= guessLimit; countGuess++) {
问题出在终止条件上。因为它只检查相等的值,所以它从不运行循环。
所以我使用 JOptionPane 制作了一个猜数游戏程序,我的问题是当我 运行 程序没有显示 window即使我构建它时没有错误,程序也会结束。
到目前为止我的代码是这样的。
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class TestNo4 {
public static void main(String[] args) {
Random ranNum = new Random();
int secretNum = ranNum.nextInt(100) + 1;
int guess = 0;
int guessNo = 0;
int guessLimit = 5;
JTextField numUserIn = new JTextField(5);
JPanel getInput = new JPanel();
getInput.add(new JLabel("Enter you guess(Limit of 5 guesses)"));
getInput.add(numUserIn);
for (int countGuess = 0; countGuess == guessLimit; countGuess++) {
int numInput = JOptionPane.showConfirmDialog(null, getInput,
"Guess number " + countGuess, JOptionPane.OK_CANCEL_OPTION);
if (numInput == JOptionPane.OK_OPTION) {
guess = Integer.parseInt(numUserIn.getText());
if (guess == secretNum) {
JOptionPane.showMessageDialog(null, "Congratulations!!",
"Got it in " + countGuess + " tries",
JOptionPane.INFORMATION_MESSAGE);
break;
}
if (countGuess == guessLimit) {
JOptionPane.showMessageDialog(null,
"Sorry, No more guesses left.", "The number is"
+ secretNum,
JOptionPane.INFORMATION_MESSAGE);
break;
}
if (guess > secretNum) {
JOptionPane.showMessageDialog(null, "Hint",
"Try something lower.",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Hint",
"Try something higher.",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
有什么地方出错了吗?
更改以下行即可
for (int countGuess = 0; countGuess == guessLimit; countGuess++) {
和
for (int countGuess = 0; countGuess <= guessLimit; countGuess++) {
问题出在终止条件上。因为它只检查相等的值,所以它从不运行循环。