2012-12-20 63 views
0

我想提出一個劊子手遊戲,這是我的代碼的一部分,所有的問題都是:'.class'錯誤無法識別?

//play method 
    private void play() throws IOException 
    { 
     guess(alphabet); 

     usedLetters(letterUsed); 

     do{ 
      centreln("You have already guessed this " + alphabet + " and the others listed above."); 
      guess(); 
     } while (letterUsed == true);//will keep asking until letter was not used 

     if (letterUsed == false) 
     { 
      life--; 
      underWord(); 
     } //only if the letter was not used, it will be checked for in the current word trying to be solved 

     if (life == 0) 
     { 
      checkWord(currentWord[] , checkLetter[]); 
     } 
    }//End of play method 

// ------------------- -------------------------------------------------- -------------------------------------------------- -------

//maskWord method to return the word with all characters not in letters replaced by _'s 
private void underWord(char currentWord[] , char checkLetter[]) throws IOException //add parameters what it recieves 
{ 

    for (int i = 0; i<currentWord.length; i++) { 
     for (int index = 0; index<checkLetter.length; index++){ 
      if (used.contains(currentWord.charAt(i))) 
      { 
       checkLetter[index] = currentWord.charAt(i); 
      } 

      else 
      { 
       checkLetter[index] = '_'; 
      } 
     } 
    } 
    for (int i = 0; i < checkLetter.length; i++) 
    { 
     System.out.println(checkLetter[i]); 
    } 
}//End of maskWord method 

// ---------------------------------- -------------------------------------------------- ------------------------------------------

//guess method to ask for user's guess 
private void guess(char used[]) throws IOException//add parameters what it recieves 
{ 
    centreln("Guess a letter in this 5-letter word: "); 
    alphabet = kb.readline().charAt[0]; 
    used[dataSize] = alphabet; 
    //adds guess to used letters array 

}//End of guess method 

// ---------------------------------------------- -------------------------------------------------- ------------------------------

//usedLetters method to check if user's guess has already been guessed 
private boolean usedLetters(char used[]) throws IOException//add parameters what it recieves 
{ 
    boolean letterUsed = false; 
    for(int x=0; x<used.length; x++) 
    { 
     if(alphabet == used[x]) 
     { 
      letterUsed = true; 
     } 
     else 
     { 
      letterUsed = false; 
     } 
    } 
    return letterUsed; 
    for (int index = 0; index < used.length; index++) 
    System.out.println(used[index]); 
    //Or could i just do centreln(usedAlphabet) 
}//End of usedLetters method 

// ----------- -------------------------------------------------- -------------------------------------------------- ---------------

//checkWord method to see if the user has got the correct word 
private void checkWord() throws IOException 
{ 
    for(int x=0; x<currentWord.length; x++) 
    { 
     if(checkLetter.equals(currentWord)) 
     { 
      centreln("HUZZAH! You have guessed the word!"); 
      centreln("You have completed this game of hangman."); 
      menu(); 
     } 

     { 
      centreln("You have run out of guesses!"); 
      centreln("The word was:"); 
       for (int index = 0; index < currentWord.length; index++) 
       { 
        System.out.print(currentWord[index]); 
       } 
      menu(); 
     } 
    } 
}//End of checkWord method 

}

// --------------------- -------------------------------------------------- -------------------------------------------------- -----

所以我想出了這個錯誤:

C:\Users\HOME\Documents\twoHangman\src\twoHangman.java:272: error: '.class' expected 
      checkWord(currentWord[] , checkLetter[]); 
            ^
C:\Users\HOME\Documents\twoHangman\src\twoHangman.java:272: error: '.class' expected 
      checkWord(currentWord[] , checkLetter[]); 
               ^

我不知道什麼是錯的...請幫助!

+1

沒有ü將其保存爲*。 Java然後你做了準備.class文件? – DRastislav

+1

你是如何運行程序的?通常使用編譯器(例如JDK)編譯Java源文件,然後使用運行時環境(例如JRE)執行Java源文件。 – McDowell

+0

@DRastislav,@McDowell:你可以在錯誤信息中看到OP正在嘗試編譯'twoHangman.java' – buc

回答

1

在你的第一個代碼塊,播放方法,你有這樣的方法調用:

checkWord(currentWord[] , checkLetter[]); 

但是看看校驗碼()的方法簽名:

private void checkWord() throws IOException 

在哪裏參數?

是你的代碼編譯?

+0

我被告知當你調用一個方法時,在方括號中,你命名了方法發送的變量並在方法簽名中,您將該方法接收的變量命名爲......這是正確的 – Selvart

+0

。你的錯誤是你沒有定義方法在你的方法定義中接收到的_parameters_。另外,'('和')'被稱爲括號(不是括號)。 – jahroy

+0

我修復了方法傳遞和接收的參數,我編譯並提出了一些新的錯誤。我需要獲取用戶的輸入,然後查看他們的輸入是否在他們猜測的單詞中,然後我需要將他們已經猜到的數據存儲在數組中,然後顯示他們未猜測和強調的內容。 – Selvart

3

我覺得在你的play()方法的問題。取而代之的

if (life == 0) 
{ 
    checkWord(currentWord[] , checkLetter[]); 
} 

你應該寫

if (life == 0) 
{ 
    checkWord(currentWord, checkLetter); 
} 

傳遞數組作爲參數。

正如Kent指出的那樣,另一個錯誤是在您的checkWord方法中。取而代之的

private void checkWord() throws IOException 

你應該寫

private void checkWord(char currentWord[], char checkLetter[]) throws IOException 

聲明,該方法需要字符數組類型的兩個參數。

0

你有一個方法checkWord定義,不接受任何參數,但是當你調用該方法,你傳遞2參數checkWord(currentWord[] , checkLetter[]);你需要定義一個方法,需要兩個參數。此外,當你調用checkWord(currentWord[], checkLetter[])應該checkWord(currentWord, checkLetter)

編輯回答問題:解決您的問題,是更新您的checkWord定義這樣 方式一:

private void checkWord(String[] currentWord, String[] checkLetter) throws IOException 
+0

如何定義一個需要兩個參數的方法? – Selvart

+0

我已經更新了我的答案。我還注意到,當您最初嘗試調用checkWord時,您正在傳遞,我認爲您打算成爲一個數組,但是,當您在checkWord時使用checkletter作爲字符串。您將需要更改定義以反映任何校驗字母應該是的。 – scrappedcola