我对Java很新,我正在制作一个在控制台中运行的小游戏。 如果有人想再次玩,我需要调用这个方法。Java无法解析为变量或不适用于参数(布尔值)
printGrid(A1, A2, A3, B1, B2, B3, C1, C2, C3);
我在下面想这(其中转和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'的? – Maroun 2014-12-09 13:36:33
错误消息告诉你,当你调用它时,你的方法期望接收*两个*布尔参数,但你只给它一个。在Java中,方法参数不是可选的,如果你的签名需要它,你必须提供它。 – JonK 2014-12-09 13:38:09
你为什么使用开关盒?简单的if/else语句会更容易。也看看String.equalsIgnoreCase()。你可以用一个陈述来替换所有的case语句,说明if(playerChoice.equalsIgnoreCase(“n”)|| playerChocie.equalsIgnoreCase(“no”)){...}。 'turn = true'和'newGame = true'语句将会进入else块 – DerStrom8 2014-12-09 13:41:13