Why while (true) loop runs only once - Java

I read server-side similar questions, but these answers did not help me with my problem: This is a short (true) loop for playing tactacto and should work all the time. But it only works after I tested its EXCEPT . I find sysout somewhere in a 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 works all the time more than 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 understand why: / I tried a lot of things, but didn’t work .. The answer would be so cool ..

By the way, even a stranger: it works without System.out.println () in debug mode, but not in normal mode or is exported as jar ...

-5
source share
3

, , - - JButtons ( X O).

, while, IF, .

0

, . , . , ​​, BlueJ, .

-1

, while() . , , gameover.

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

All Articles