[英]Why does while(true) loop just run once - Java
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.我阅读了几个类似的问题,但这些答案无法帮助我解决我的问题:这是一个 tictactoe 游戏的 while(true) 循环,应该一直运行。 But it only runs once I tested it EXCEPT I type a sysout somewhere in the loop...(not in one of the if statements):
但它只在我测试后运行,除了我在循环中的某处键入一个 sysout ......(不是在 if 语句之一中):
Doesn't work like this:不是这样工作的:
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);
}
}
}
but it works like this (sysout at the end) so it runs the whole time not just once :但它的工作原理是这样的(最后是 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();
}
}
I just don't get why :/ Tried many things but didn't work.. Answer would be so cool..我只是不明白为什么:/尝试了很多东西但没有用..答案会很酷..
by the way even stranger: It works without System.out.println() in debug mode but not in normal mode or exported as jar...顺便说一句,更奇怪的是:它在调试模式下没有 System.out.println() 工作,但不在正常模式下或作为 jar 导出......
You should check do you have winner every time when someone click on any JButtons (I thought on X and O buttons).每次有人单击任何 JButton 时,您都应该检查是否有赢家(我想到了 X 和 O 按钮)。
So you don't need while loop you can improve IF to be more simples.所以你不需要 while 循环,你可以将 IF 改进为更简单。
There may be some problem in your second class that ends the game.你的第二堂课可能有一些问题导致游戏结束。 A simple typo, most likely.
一个简单的错字,很可能。 Might want to find a program such as BlueJ that has the capability to execute one class at a time.
可能想要找到一个程序,例如 BlueJ,它能够一次执行一个类。
When you make it like this, the while() would run forever.当你这样做时,while() 将永远运行。 You should make a boolean and name it for example gameover.
您应该创建一个布尔值并将其命名为例如 gameover。
boolean gameover = false;
while(!gameover){
//your gameloop
if(yourgameovercondition){
gameover = true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.