有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JButton操作中if语句的java问题

我对java相当陌生,我刚刚制作了一个TictaToe程序。我成功地用人工智能创建了它,但当我试图添加一个额外的选项来与另一个人玩时,我遇到了一个问题。我试图这样做,如果turncount(定义为静态int)是偶数,它将检查按下按钮的文本是否等于零,如果是,则将其设置为x。否则,它应该检查按下按钮的文本是否等于零,如果是,则将其设置为O。无论出于何种原因,它似乎不会检查else语句。下面的代码非常重复,所以您只需要阅读前12行左右的内容就可以了解我的意思。谢谢你的帮助

操作侦听器方法:

public void actionPerformed(ActionEvent e) {
        if (e.getSource() == topl)
            if (turncount % 2 == 0)
            {
                if (topl.getText().equals(""))
                {
                    topl.setText("X");
                    turncount  += 1;
                    winchecker();

                }
            else
                if (topl.getText().equals(""))
                {
                    topl.setText("O");
                    turncount  += 1;
                    winchecker();

                }
            }
        if (e.getSource() == midup)
        {
            if (turncount % 2 == 0)
            {
            if (midup.getText().equals(""))
            {
                midup.setText("X");
                turncount  += 1;
                winchecker();

            }
            else
                if (midup.getText().equals(""))
                {
                    midup.setText("O");
                    turncount  += 1;
                    winchecker();

                }
            }
        }
        if (e.getSource() == topr)
        {
            if (turncount % 2 == 0)
            {
            if (topr.getText().equals(""))
            {
                topr.setText("X");
                turncount  += 1;
                winchecker();

            }
            else
                if (topr.getText().equals(""))
                {
                    topr.setText("O");
                    turncount  += 1;
                    winchecker();

                }
            }
        }
        if (e.getSource() == midl)
        {
            if (turncount % 2 == 0)
            {
            if (midl.getText().equals(""))
            {
                midl.setText("X");
                turncount  += 1;
                winchecker();

            }
            else
                if (midl.getText().equals(""))
                {
                    midl.setText("O");
                    turncount  += 1;
                    winchecker();
                }
            }
        }
        if (e.getSource() == mid)
        {
            if (turncount % 2 == 0)
            {
            if (mid.getText().equals(""))
            {
                mid.setText("X");
                turncount  += 1;
                winchecker();

            }
            else
                if (mid.getText().equals(""))
                {
                    mid.setText("O");
                    turncount  += 1;
                    winchecker();

                }
            }           }
        if (e.getSource() == midr)
        {
            if (turncount % 2 == 0)
            {
            if (midr.getText().equals(""))
            {
                midr.setText("X");
                turncount  += 1;
                winchecker();

            }
            else
                if (midr.getText().equals(""))
                {
                    midr.setText("O");
                    turncount  += 1;
                    winchecker();

                }
            }
        }
        if (e.getSource() == botl)
        {
            if (turncount % 2 == 0)
            {
            if (botl.getText().equals(""))
            {
                botl.setText("X");
                turncount  += 1;
                winchecker();

            }
            else
                if (botl.getText().equals(""))
                {
                    botl.setText("O");
                    turncount  += 1;
                    winchecker();

                }
            }
        }
        if (e.getSource() == midlow)
        {
            if (turncount % 2 == 0)
            {
            if (midlow.getText().equals(""))
            {
                midlow.setText("X");
                turncount  += 1;
                winchecker();

            }
            else
                if (midlow.getText().equals(""))
                {
                    midlow.setText("O");
                    turncount  += 1;
                    winchecker();

                }
            }
        }
        if (e.getSource() == botr)
        {
            if (turncount % 2 == 0)
            {
            if (botr.getText().equals(""))
            {
                botr.setText("X");
                turncount  += 1;
                winchecker();

            }
            else
                if (botr.getText().equals(""))
                {
                    botr.setText("O");
                    turncount  += 1;
                    winchecker();

                }
            }
        }
    }

共 (2) 个答案

  1. # 1 楼答案

    if the turncount (defined as a static int) is an even number, it will check if the pressed button's text is equal to nothing, and if so set it to x. Otherwise, it should check to see if the pressed button's text is equal to nothing, and if so set it to O.

    如果您的意思是,如果turncount是奇数,则需要设置O;然后您需要一对额外的大括号{},因为如果没有大括号else块将实际匹配内部if块而不是外部(因此永远不会执行,因为它们匹配的条件与文本是""相同)

            if (turncount % 2 == 0)
            {
                if (topl.getText().equals(""))
                {
                    topl.setText("X");
                    turncount  += 1;
                    winchecker();
    
                }
            } // ADDED
            else
            { // ADDED
                if (topl.getText().equals(""))
                {
                    topl.setText("O");
                    turncount  += 1;
                    winchecker();
                }
            }
    
  2. # 2 楼答案

    你的else语句属于内部if语句,而不是你想要的外部if语句。您当前的代码本质上是一个测试if-topl的大if块。getText()。等于(“”)两次,这没有意义。所以你有这个:

            if (turncount % 2 == 0)
            {
                if (topl.getText().equals(""))
                {
                    topl.setText("X");
                    turncount  += 1;
                    winchecker();
    
                }
            else 
                if (topl.getText().equals(""))
                {
                    topl.setText("O");
                    turncount  += 1;
                    winchecker();
    
                }
            }
    

    您应该将其更改为以下内容:

            if (turncount % 2 == 0)
            {
                if (topl.getText().equals(""))
                {
                    topl.setText("X");
                    turncount  += 1;
                    winchecker();
    
                }
            }
            else {
                if (topl.getText().equals(""))
                {
                    topl.setText("O");
                    turncount  += 1;
                    winchecker();
    
                }
            }