2013-07-16 109 views
0

我正在努力處理這個非常簡單的代碼:我試圖打印出「_」標記,用戶輸入的單詞中的每個字母都帶有一個_標記。然而,每當我嘗試編譯代碼時,我都會得到「錯誤:類game_3中的方法makeLine不能應用於給定類型;原因:實際和正式參數列表的長度不同。」Hang子手遊戲:實際和正式的參數列表長度不一

這似乎很清楚的反饋,但我不認爲我真的明白它 - 起初,我認爲這是因爲我沒有給stringNumber賦值,但給它賦值並沒有幫助。怎麼了?

/*Assignment: 
Write a reverse Hangman game in which the user thinks of a word and the computer tries 
to guess the letters in that word. Your program must output what the computer guessed 
on each turn, and show the partially completed word. It also must use pseudorandom 
functions to make guesses. That is, it should not simply try all the letters in order, 
nor should it use the user’s input to its advantage. 
*/ 

import java.util.*; 

    public class game_3 { 

    public static void main(String[] args) { 

      getIntroduction(); 
      playGame(); 

    } 

    public static void getIntroduction(){ 
     System.out.println(); 
     System.out.println(); 
     System.out.println("*************************"); 
     System.out.println("Welcome to Hangman"); 
     System.out.println("In this game, you'll provide a word for the computer to guess."); 
     System.out.println(); 
     System.out.println("The computer will guess letters randomly, and assess whether"); 
     System.out.println("they can be used to complete your word."); 
     System.out.println(); 
     System.out.println("Let's play!"); 
     System.out.println(); 
     System.out.println("*************************"); 
     System.out.println(); 

    } 

    public static void playGame(){ 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please enter a word: "); 
     String hangWord = input.next(); 
     int stringNum = hangWord.length(); 

     makeLine(); 
    } 

    public static void makeLine(int stringNum){ 
    for (int i = 0; i < stringNum; i++){ 
     System.out.print("_ "); 
    } 
    } 
} 

回答

1

的方法makeline預計爲int的參數:

public static void makeLine(int stringNum){ 

你不帶任何參數調用它:

makeLine(); 

是什麼樣子,你想要的是:

makeLine(stringNum); 

編輯:要清楚,那是wh在錯誤消息是指正式參數列表(預期)和實際參數列表(你給了它什麼)。當你給出一個方法的時候發生的另一個常見的錯誤信息與它期望的不一致是「方法methodName(expected args)不適用於參數(給定的參數)。當類型不匹配時會發生這種情況:如果您在傳遞一個字符串時它需要一個int,或者如果您傳遞了正確的類型,但是沒有順序。

相關問題