2012-12-20 283 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