我是java的新手,我刚刚做了一个TicTacToe程序。我成功地只用人工智能创建了它,但是当我试图添加一个额外的选项来与另一个人一起玩时,我遇到了一个问题。我试图使它成为如果转数(定义为静态 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();
}
}
}
}
如果 turncount(定义为静态 int(是偶数,它将检查按下按钮的文本是否等于无,如果是,则将其设置为 x。 否则,它应该检查按下按钮的文本是否等于无,如果是,则将其设置为 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();
}
}
你的 else 语句属于 INNER if 语句,而不是你想要的外部语句。你当前的代码本质上是一个大的if块,它测试topl.getText((.equals("(两次,这没有意义。所以你有这个:
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();
}
}