2014-12-09 31 views
0

我對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上查看類似的答案後,我仍然無能爲力。

+0

你是怎麼稱呼'xWins'的? – Maroun 2014-12-09 13:36:33

+4

錯誤消息告訴你,當你調用它時,你的方法期望接收*兩個*布爾參數,但你只給它一個。在Java中,方法參數不是可選的,如果你的簽名需要它,你必須提供它。 – JonK 2014-12-09 13:38:09

+1

你爲什麼使用開關盒?簡單的if/else語句會更容易。也看看String.equalsIgnoreCase()。你可以用一個陳述來替換所有的case語句,說明if(playerChoice.equalsIgnoreCase(「n」)|| playerChocie.equalsIgnoreCase(「no」)){...}。 'turn = true'和'newGame = true'語句將會進入else塊 – DerStrom8 2014-12-09 13:41:13

回答

0

xWins方法正在尋找兩個布爾參數。您不會告訴我們您是如何調用該方法的,但是由於錯誤,顯然您只是試圖將它傳遞給一個布爾值。由於該名稱的方法不存在只接受一個參數,它不能繼續 - 你創建的方法是尋找兩個布爾參數不會工作,如果你只是試圖通過它。

+0

這是一個鏈接到整個事情(沒有在新遊戲開始時調用printGrid): https://gist.github.com/SarahJessica/e75facaf5637b6c7fe2b 通過TBH瀏覽是很多東西。 :/ – 2014-12-09 13:52:54

+0

看看第84和91行 - 你只傳遞一個布爾值。你也應該把它傳遞給你的「newGame」變量。或者從方法聲明中刪除第二個布爾參數,但只有當你不打算在xWins()內使用「newGame」時纔有效() – DerStrom8 2014-12-09 13:55:59

+0

從我看到的你不需要將「newGame」傳入方法,所以我會把它從聲明中拿出來,只使用'private static void xWins(boolean turn){...}' – DerStrom8 2014-12-09 14:03:16