这是我的程序的大部分错误部分:
//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();
}
}