【问题标题】:Java cannot be resolved to a variable or not applicable for the arguments (boolean)Java 无法解析为变量或不适用于参数(布尔值)
【发布时间】:2014-12-09 13:35:27
【问题描述】:

我对 Java 相当陌生,我正在制作一个在控制台中运行的小游戏。 如果有人想再玩,我需要调用这个方法。

printGrid(A1, A2, A3, B1, B2, B3, C1, C2, C3);

我在下面尝试过(其中 turn 和 newGame 被声明为全局变量:

private static void xWins(boolean turn, boolean newGame) {
    String playerChoice;
    System.out.println("Player X Wins!");
    System.out.println("|Would you like to play again? |");
    System.out.println("|Enter Y or N                  |");
        playerChoice = enterChoice(turn);
            switch(playerChoice){
            case "n": thankYou();break;
            case "N": thankYou();break;
            case "no": thankYou(); break;
            case "No": thankYou();break;
            case "NO": thankYou();break;
            case "nO": thankYou();break;
            default: turn = true; newGame = true;
            }
}

我试图让它与这个一起工作,它在主循环中与这个一起工作

if (newGame == true) printGrid(A1, A2, A3, B1, B2, B3, C1, C2, C3);

我在 Eclipse 中收到的错误消息是

The method xWins(boolean, boolean) in the type ticTacToe is not applicable for the arguments (boolean)

否则,如果我尝试在课程结束时直接调用printGrid 而不是newGame = true;,则会收到消息告诉我这些全局定义的变量中的每一个都无法解析为变量。将其设为公共课程也没有什么区别。在 StackOverflow 上寻找类似的答案后,我仍然一无所知。

【问题讨论】:

  • 你怎么打电话给xWins
  • 错误消息告诉您,您的方法在调用它时期望接收 两个 布尔参数,但您只给它一个。方法参数在 Java 中不是可选的,如果您的签名需要它,您必须提供它。
  • 为什么要使用开关盒?一个简单的 if/else 语句会容易得多。还要查看 String.equalsIgnoreCase()。您可以用一个声明if (playerChoice.equalsIgnoreCase("n") || playerChocie.equalsIgnoreCase("no")) {...} 替换所有案例声明。然后 turn = truenewGame = true 语句将进入 else 块
  • 谢谢,我会尝试使用 if else 语句。
  • @Maroun 没错,我的称呼是错误的。感谢您的帮助。

标签: java eclipse class methods


【解决方案1】:

xWins 方法正在寻找两个布尔参数。您没有向我们展示您是如何调用该方法的,但是通过错误很明显您只是试图向它传递一个布尔值。由于该名称的方法不存在只接受一个参数,因此它无法继续 - 如果您只尝试传递一个参数,您创建的寻找两个布尔值作为参数的方法将不起作用。

【讨论】:

  • 这是一个链接到整个事情(没有在新游戏开始时调用 printGrid):gist.github.com/SarahJessica/e75facaf5637b6c7fe2b TBH 需要查看很多内容。 ://
  • 查看第 84 和 91 行 - 你只传递了一个布尔值。您还应该将“newGame”变量传递给它。要么从方法声明中删除第二个布尔参数,但仅当您不打算在 xWins() 中使用“newGame”时才有效
  • 据我所知,您不需要将“newGame”传递给方法,所以我将其从声明中取出,只使用private static void xWins(boolean turn) {...}
  • 非常感谢。这似乎是解决了问题。我很尴尬,因为这是一个如此明显的疏忽!非常非常感谢您的帮助!
  • 别担心!很高兴我们能提供帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多