简体   繁体   中英

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. But it only runs once I tested it EXCEPT I type a sysout somewhere in the loop...(not in one of the if statements):

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 :

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

You should check do you have winner every time when someone click on any JButtons (I thought on X and O buttons).

So you don't need while loop you can improve IF to be more simples.

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.

When you make it like this, the while() would run forever. You should make a boolean and name it for example gameover.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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