我是java的新手,我刚刚制作了一个TicTacToe程序。我成功地设法用AI创建了它,但是当我试图添加一个额外的选项来与另一个人玩时,我遇到了一个问题。我试图使它成为如果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();
}
}
}
}
答案 0 :(得分:2)
你的else语句属于INNER if语句,而不是你想要的外语。你当前的代码本质上是一个大的if-block,它测试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();
}
}
答案 1 :(得分:2)
如果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();
}
}