Tic Tac Toe Game错误

时间:2012-12-20 21:45:55

标签: java

我编写了一个tic tac toe游戏,它编译并运行良好,但是如果你点击左上角的框,它会自动说X赢得Like this。这是董事会唯一能做到这一点的运动。不确定在确定获胜者时我做错了什么。如果您想要查看更多代码,则完整代码为here

        //Determine who won
        if(button1.getText() == button2.getText()
                && button2.getText() == button3.getText()
                && button1.getText() != "")
        {
            win = true;
        }
        else if(button4.getText() == button5.getText()
                    && button5.getText() == button6.getText()
                    && button4.getText() != "")
        {
            win = true;
        }
        else if(button7.getText() == button8.getText()
                    && button8.getText() == button9.getText()
                    && button7.getText() != "")
        {
            win = true;
        }

        else if(button1.getText() == button4.getText()
                    && button4.getText() == button7.getText()
                    && button1.getText() != "")
        {
            win = true;
        }
        else if(button2.getText() == button5.getText()
                    && button5.getText() == button8.getText()
                    && button2.getText() != "")
        {
            win = true;
        }
        else if(button3.getText() == button6.getText()
                        && button6.getText() == button9.getText()
                        && button3.getText() != "")
        {
                win = true;
        }
        else if(button1.getText() == button5.getText()
                    && button5.getText() == button9.getText()
                    && button1.getText() != "")
        {
            win = true;
        }
        else if(button3.getText() == button5.getText()
                    && button5.getText() == button7.getText()
                    && button1.getText() != "")
        {
            win = true;
        }
        else
        {
            win = false;
        }

        if (win == true)
        {
            JOptionPane.showMessageDialog(null,
                                          letter + " Wins!");
        }
        else if (count == 9 && win == false)
        {
            JOptionPane.showMessageDialog(null,
                                          "Tie Game!");
        }
    }
}

1 个答案:

答案 0 :(得分:4)

请勿使用==!=比较字符串。请改用equals()!equals()

请参阅How do I compare strings in Java?