将布尔结果传递给另一个方法if语句。 (tic-tac-toe游戏)

时间:2015-05-15 18:31:29

标签: java if-statement methods tic-tac-toe

public boolean isWinner(char player)
{
        if (board[0] == player && board[1] == player && board[2] == player ||
            board[3] == player && board[4] == player && board[5] == player ||
            board[6] == player && board[7] == player && board[8] == player ||
            board[0] == player && board[3] == player && board[6] == player ||
            board[1] == player && board[4] == player && board[7] == player ||
            board[2] == player && board[5] == player && board[8] == player )
            return true;

  return false;  
}
/* check to see if the player x is the winner or
   the player y is the winner or the cat is the winner 
    or the game is not over yet  and then display the result
     you need to write conditional statments*/
public void displayResults()
{
  if (isWinner = true)
     System.out.print("CONGRATUTIONS " + player + " YOU WON!");
}

嘿,我想知道是否有人可以帮助我如何将“isWinner”结果传递给“displayResults”if语句。如果这是我需要帮助的部分之一,那么这是我们获得分配的井字游戏。

2 个答案:

答案 0 :(得分:1)

假设player是可访问的char对象...

public void displayResults()
{
  if (isWinner(player))
     System.out.print("CONGRATULATIONS " + player + " YOU WON!");
                          // ^^^^
                          // Fix your typo too
}

答案 1 :(得分:0)

首先,isWinner应该返回truefalse,而不是1或0。

其次,

if (isWinner = true)

应该是

if (isWinner(player))

假设player是代表玩家的字符,因为这是isWinner()所需要的。