0

这是我的程序的大部分错误部分:

    //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();
    }
}
4

3 回答 3

1

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

于 2012-12-21T02:36:44.387 回答
1

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


更重要的是,currentWord是一个char数组。要访问 index 处的字符i,您可以使用currentWord[i]而不是currentWord.charAt(i).

此外,您不能使用used.contains(anything)sinceused是一个数组。尝试循环used检查是否匹配。

于 2012-12-21T02:16:47.827 回答
1

正如@irrelephant 指出的那样:你拼错了readLine()

L必须大写。


另一个错误消息也非常简单。

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

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

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

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

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


学习如何自己解决这些错误会更好。

每次遇到编译错误时都来 StackOverflow 浪费您和我们的时间。

于 2012-12-21T02:17:11.977 回答