Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
954 views
in Technique[技术] by (71.8m points)

swing - 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) && 
                    (

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...