为什么 while(true) 循环只运行一次 - Java [英] Why does while(true) loop just run once - Java
问题描述
我阅读了几个类似的问题,但这些答案无法帮助我解决我的问题:这是一个 tictactoe 游戏的 while(true) 循环,应该一直运行.但它只在我测试后运行 EXCEPT 我在循环中的某处输入一个 sysout ......(不是在一个 if 语句中):
I read serveral similar questions but those answers couldnt help me with my problem: This is a while(true) loop for a tictactoe game and should run the whole time. But it only runs once I tested it EXCEPT I type a sysout somewhere in the loop...(not in one of the if statements):
不是这样工作的:
void winCheck() {
while(true) {
if(buttons[0].getValue() == 1 && buttons[1].getValue() == 1 && buttons[2].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[3].getValue() == 1 && buttons[4].getValue() == 1&& buttons[5].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[6].getValue() == 1 && buttons[7].getValue() == 1 && buttons[8].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[0].getValue() == 1 && buttons[3].getValue() == 1 && buttons[6].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[1].getValue() == 1 && buttons[4].getValue() == 1 && buttons[7].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[2].getValue() == 1 && buttons[5].getValue() == 1 && buttons[8].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[0].getValue() == 1 && buttons[4].getValue() == 1 && buttons[8].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[6].getValue() == 1 && buttons[4].getValue() == 1 && buttons[2].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[0].getValue() == 2 && buttons[1].getValue() == 2 && buttons[2].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[3].getValue() == 2 && buttons[4].getValue() == 2 && buttons[5].getValue() ==2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[6].getValue() ==2 && buttons[7].getValue() == 2 && buttons[8].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[0].getValue() == 2 && buttons[3].getValue() == 2 && buttons[6].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[1].getValue() == 2 && buttons[4].getValue() == 2 && buttons[7].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[2].getValue() == 2 && buttons[5].getValue() == 2 && buttons[8].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[0].getValue() == 2 && buttons[4].getValue() == 2 && buttons[8].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(buttons[6].getValue() == 2 && buttons[4].getValue() == 2 && buttons[2].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
} else if(
(buttons[0].getValue() == 1 || buttons[0].getValue() == 2) &&
(buttons[1].getValue() == 1 || buttons[1].getValue() == 2) &&
(buttons[2].getValue() == 1 || buttons[2].getValue() == 2) &&
(buttons[3].getValue() == 1 || buttons[3].getValue() == 2) &&
(buttons[4].getValue() == 1 || buttons[4].getValue() == 2) &&
(buttons[5].getValue() == 1 || buttons[5].getValue() == 2) &&
(buttons[6].getValue() == 1 || buttons[6].getValue() == 2) &&
(buttons[7].getValue() == 1 || buttons[7].getValue() == 2) &&
(buttons[8].getValue() == 1 || buttons[8].getValue() == 2)) {
dispose();
JOptionPane.showMessageDialog(null, "Draw..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
}
}
但它的工作原理是这样的(最后是 sysout),所以它会一直运行而不仅仅是一次:
void winCheck() {
while(true) {
if(buttons[0].getValue() == 1 && buttons[1].getValue() == 1 && buttons[2].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[3].getValue() == 1 && buttons[4].getValue() == 1&& buttons[5].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[6].getValue() == 1 && buttons[7].getValue() == 1 && buttons[8].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[0].getValue() == 1 && buttons[3].getValue() == 1 && buttons[6].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[1].getValue() == 1 && buttons[4].getValue() == 1 && buttons[7].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[2].getValue() == 1 && buttons[5].getValue() == 1 && buttons[8].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[0].getValue() == 1 && buttons[4].getValue() == 1 && buttons[8].getValue() == 1) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[6].getValue() == 1 && buttons[4].getValue() == 1 && buttons[2].getValue() == 1 ) {
dispose();
JOptionPane.showMessageDialog(null, "Crosses win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[0].getValue() == 2 && buttons[1].getValue() == 2 && buttons[2].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[3].getValue() == 2 && buttons[4].getValue() == 2 && buttons[5].getValue() ==2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[6].getValue() ==2 && buttons[7].getValue() == 2 && buttons[8].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[0].getValue() == 2 && buttons[3].getValue() == 2 && buttons[6].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[1].getValue() == 2 && buttons[4].getValue() == 2 && buttons[7].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[2].getValue() == 2 && buttons[5].getValue() == 2 && buttons[8].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[0].getValue() == 2 && buttons[4].getValue() == 2 && buttons[8].getValue() == 2 ) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(buttons[6].getValue() == 2 && buttons[4].getValue() == 2 && buttons[2].getValue() == 2) {
dispose();
JOptionPane.showMessageDialog(null, "Noughts win..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
else if(
(buttons[0].getValue() == 1 || buttons[0].getValue() == 2) &&
(buttons[1].getValue() == 1 || buttons[1].getValue() == 2) &&
(buttons[2].getValue() == 1 || buttons[2].getValue() == 2) &&
(buttons[3].getValue() == 1 || buttons[3].getValue() == 2) &&
(buttons[4].getValue() == 1 || buttons[4].getValue() == 2) &&
(buttons[5].getValue() == 1 || buttons[5].getValue() == 2) &&
(buttons[6].getValue() == 1 || buttons[6].getValue() == 2) &&
(buttons[7].getValue() == 1 || buttons[7].getValue() == 2) &&
(buttons[8].getValue() == 1 || buttons[8].getValue() == 2)
)
{
dispose();
JOptionPane.showMessageDialog(null, "Draw..", "TicTacToe", JOptionPane.INFORMATION_MESSAGE);
Start.main(null);
}
System.out.println();
}
}
我就是不明白为什么:/尝试了很多东西但没有奏效..答案会很酷..
顺便说一句,更奇怪的是:它在调试模式下没有 System.out.println() 也能工作,但在正常模式下或作为 jar 导出...
推荐答案
当有人点击任何 JButtons(我想到 X 和 O 按钮)时,你应该检查你是否有赢家.
You should check do you have winner every time when someone click on any JButtons (I thought on X and O buttons).
所以你不需要 while 循环,你可以改进 IF 使其更简单.
So you don't need while loop you can improve IF to be more simples.
这篇关于为什么 while(true) 循环只运行一次 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!