gpt4 book ai didi

java - Hangman 程序,缓冲读取器出现错误 : cannot find symbol,? Java新手

转载 作者:行者123 更新时间:2023-12-02 00:05:26 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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

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

关于java - Hangman 程序,缓冲读取器出现错误 : cannot find symbol,? Java新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13983329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com