“.class”错误无法识别?

时间:2012-12-20 22:48:25

标签: java

我正在制作一个刽子手游戏,这是我的代码中所有问题的一部分:

    //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[]);
                                                   ^

我不确定有什么问题...请帮忙!

3 个答案:

答案 0 :(得分:3)

我认为您的play()方法存在问题。而不是

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

你应该写

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

将数组作为参数传递。

正如肯特指出的那样,另一个错误在于你的checkWord方法。而不是

private void checkWord() throws IOException

你应该写

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

声明此方法采用两个字符数组类型的参数。

答案 1 :(得分:1)

在你的第一个代码块play方法中,你有这个方法调用:

checkWord(currentWord[] , checkLetter[]);

但看看checkWord()的方法签名:

private void checkWord() throws IOException

参数在哪里?

您的代码是否已编译?

答案 2 :(得分:0)

您定义的方法checkWord不接受任何参数,但是当您调用该方法时,您传递的是2个参数checkWord(currentWord[] , checkLetter[]);,您需要定义一个需要两个参数的方法。另外,当您致电checkWord(currentWord[], checkLetter[])时,它应该是checkWord(currentWord, checkLetter)

编辑回答问题: 解决问题的一种方法是以这种方式更新checkWord的定义:

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