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

时间:2016-04-15 21:02:12

标签: java swing

我阅读了几个类似的问题,但这些答案无法解决我的问题: 这是一个tictactoe游戏的while(true)循环,应该在整个时间运行。但它只运行一次我测试它 EXCEPT 我在循环中的某个地方输入一个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 ...

3 个答案:

答案 0 :(得分:0)

每当有人点击任何JButtons时(我想到X和O按钮),你应该检查你是否有胜利者。

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

答案 1 :(得分:-1)

你的第二节课可能会有一些问题结束游戏。很可能是一个简单的拼写错误。可能想找到一个像BlueJ这样的程序,它能够一次执行一个类。

答案 2 :(得分:-4)

当你这样做时,while()会永远运行。 你应该制作一个布尔值,并将其命名为gameover。

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