0

我编写了一个井字游戏,它编译和运行良好,但是如果你点击左上角的框,它会自动显示 X Wins Like this。这是董事会上唯一能做到这一点的运动。不知道我在确定获胜者时做错了什么。如果您想查看更多代码,完整的代码在这里

        //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!");
        }
    }
}
4

1 回答 1

4

不要使用==and比较字符串!=。使用equals()and!equals()代替。

请参阅如何比较 Java 中的字符串?

于 2012-12-20T21:47:48.847 回答