【问题标题】:Passing a boolean result into another methods if statement. (tic-tac-toe game)将布尔结果传递给另一个方法 if 语句。 (井字游戏)
【发布时间】:2015-05-15 18:31:29
【问题描述】:
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 语句。这是一个井字游戏,如果这是我需要帮助的部分之一,我们会分配给它。

【问题讨论】:

  • 你需要看一些java的基础教程。尝试用谷歌搜索类似get return value of method java
  • 作为参数传递给displayResults() 方法?来吧,它太基础了,你需要先尝试一些java教程。
  • 当方法需要 boolean 时,你如何返回 0 和 1?
  • 是的,关于 1 和 0 抱歉,我正在处理我的一些想法,忘记恢复这些想法
  • isWinner = true 与 isWinner == true 不同

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    其次,

    if (isWinner = true)
    

    应该是

    if (isWinner(player))
    

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

    【讨论】:

    • 是的,关于 1 和 0 抱歉,我正在处理我的一些想法而忘记恢复这些想法
    猜你喜欢
    • 2020-06-16
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多