繁体   English   中英

为什么 while(true) 循环只运行一次 - Java

[英]Why does while(true) loop just run once - Java

我阅读了几个类似的问题,但这些答案无法帮助我解决我的问题:这是一个 tictactoe 游戏的 while(true) 循环,应该一直运行。 但它只在我测试后运行,除了我在循环中的某处键入一个 sysout ......(不是在 if 语句之一中):

不是这样工作的:

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 导出......

每次有人单击任何 JButton 时,您都应该检查是否有赢家(我想到了 X 和 O 按钮)。

所以你不需要 while 循环,你可以将 IF 改进为更简单。

你的第二堂课可能有一些问题导致游戏结束。 一个简单的错字,很可能。 可能想要找到一个程序,例如 BlueJ,它能够一次执行一个类。

当你这样做时,while() 将永远运行。 您应该创建一个布尔值并将其命名为例如 gameover。

boolean gameover = false;
while(!gameover){
//your gameloop
if(yourgameovercondition){
gameover = true;
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM