hangman程序,错误:找不到符号,缓冲读卡器? Java新手

时间:2012-12-21 02:06:29

标签: java

以下是我的程序中包含大部分错误的部分内容:

    //play method
    private void play() throws IOException
    {
        do {
            guess(alphabet);
            usedLetters(used, alphabet);

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

            if (letterUsed == false)
            {
                life--;
                underWord(currentWord , checkLetter, used);
            } //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);
            }
        } while (checkLetter != currentWord);

    }

此方法特别具有此刻的所有错误。我需要询问用户对1个字母的猜测,然后检查它是否在他们当前想要猜测的单词中。我还存储了他们已经猜到的所有字母,以确保他们不会因为猜两次相同的字母而失去生命。所以她有三个数组,一个用于存储用过的字母,一个用于存储已经猜到的单词部分(以及需要_的部分),另一个用于存储他们试图猜测的单词。有没有更好的方法我可以在下面编写这个方法来完成它所需的任务?

//underWord method to return the word with all characters not in letters replaced by _'s
private void underWord(char currentWord[] , char checkLetter[], char used[]) 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

我编译并显示以下内容:

C:\Users\HOME\Documents\JCreator LE\MyProjects\twoHangman\src\twoHangman.java:289: error: cannot find symbol
               if (used.contains(currentWord.charAt(i)))
                                            ^
symbol:   method charAt(int)
location: variable currentWord of type char[]

C:\Users\HOME\Documents\JCreator LE\MyProjects\twoHangman\src\twoHangman.java:291: error: cannot find symbol
                checkLetter[index] =  currentWord.charAt(i);
                                                 ^
symbol:   method charAt(int)
location: variable currentWord of type char[]

C:\Users\HOME\Documents\JCreator LE\MyProjects\twoHangman\src\twoHangman.java:312: error: cannot find symbol
    alphabet = kb.readline().charAt(0);
                 ^
symbol:   method readline()
location: variable kb of type BufferedReader

这是我第一次使用Buffered Reader,我不确定是什么问题

//guess method to ask for user's guess

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

}


//usedLetters method to check if user's guess has already been guessed
private boolean usedLetters(char used[], char alphabet) throws IOException
{
    for(int x=0; x<used.length; x++)
    {
        if(alphabet == used[x])
        {
            letterUsed = true;
            return letterUsed;
        }
        else
        {
            letterUsed = false;
            return letterUsed;
        }
     }

     for (int index = 0; index < used.length; index++) {
        System.out.println(used[index]);
    }


}


//checkWord method to see if the user has got the correct word

private void checkWord(char currentWord[], char checkLetter[]) throws IOException
{
    for(int x=0; x<currentWord.length; x++)
    {
        if(checkLetter == currentWord)
        {
            System.out.println("HUZZAH! You have guessed the word!");
            System.out.println("You have completed this game of hangman.");
            menu();
        }

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

3 个答案:

答案 0 :(得分:1)

首先,readline应为readLine - Java区分大小写。

<小时/> 更重要的是,currentWord是一个char数组。要访问索引i处的字符,您可以使用currentWord[i]代替currentWord.charAt(i)

此外,由于used.contains(anything)是一个数组,因此无法使用used。尝试循环浏览used以检查匹配。

答案 1 :(得分:1)

正如@irrelephant指出:你拼错了 readLine()错误。

L必须大写。


另一条错误信息也非常简单。

它告诉你,数组没有这样的方法 charAt()

数组在Java中没有任何方法。

但是,String 确实有一个名为 charAt()的方法

在Java中,字符数组和字符串之间存在差异。

变量 checkLetter currentWord 应该是字符串(不是字符数组)。


您可以更好地学习如何自行解决这些错误。

每次收到编译错误时都会收到StackOverflow会浪费你的时间和我们的时间。

答案 2 :(得分:1)

currentWord被声明为char currentWord[]。这意味着currentWordchar的数组。在Java中,这与String不同。特别是,数组没有任何方法。您需要使用数组下标,例如currentWord[i]而不是currentWord.charAt(i)。或者,您可以将程序设计为使用String而不是char[]