【发布时间】:2015-11-30 19:05:43
【问题描述】:
我正在写一个井字游戏问题。
TicTacToe 类工作正常,但我的 Main 类中的第一个 if 语句没有处理。 TicTacToe 类没有什么问题,因为当玩家 1 获胜时,oNew.checkWinner(1) 返回 true。 (我通过在 if 语句之前放置 System.out.println(oNew.checkWinner(1)); 来检查)。
但是它并没有传递到 if 语句来中断 while 循环,而是继续。第一个 else if 也一样。
奇怪的是它对第二个 if 语句和第二个 else if 语句工作得很好。
public static void main(String[] args) {
TicTacToe oNew = new TicTacToe();
String champ = "";
boolean hasWon = false;
oNew.getMove();
while(hasWon == false){
oNew.setMove(1);
oNew.getMove();
if (oNew.checkWinner(1) == true){
champ = "Player 1 wins.";
hasWon = true;
}
else if(oNew.checkCatsGame() == true){
champ = "Draw";
hasWon = true;
}
oNew.setMove(2);
oNew.getMove();
if (oNew.checkWinner(2) == true){
champ = "Player 2 wins.";
hasWon = true;
}
else if(oNew.checkCatsGame() == true){
champ = "Draw";
hasWon = true;
}
}
System.out.println(champ);
}
【问题讨论】:
标签: java if-statement