我是编程新手并试图搞定一个基本的猜谜游戏,但我有这个错误。需要一些帮助,因为我已将 'guess' 设置为char
,然后想将其与字符数组进行比较,但尝试了几种不同的方法,但还没有任何乐趣。
它在底部的 if 语句中给出了错误,其中包含:
(guess.equals(wordContainer[j]))
提前致谢。
我的代码:
import java.util.Scanner;
public class GuessingGame {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
String wordArray[] = {"aardvarks", "determine", "different", "greatness", "miserable", "trappings", "valuables", "xylophone"};
double rand = Math.random() * 8;
int x = 0;
x = (int)rand;
System.out.println(x);
String word = wordArray[x];
int wordCount = word.length();
System.out.println(word);
// System.out.println(wordCount);
char wordContainer[] = new char[wordCount];
char wordHiddenContainer[] = new char[wordCount];
String input;
char guess;
System.out.print("Enter your guess(a-z): ");
input = keyboard.next();
guess = input.charAt(0);
for ( int i = 0 ; i < wordCount ; i++ ) {
wordContainer[i] = word.charAt(i);
wordHiddenContainer[i] = '*';
}
System.out.println(wordContainer);
System.out.println(wordHiddenContainer);
for (int j = 0; j < word.length(); j++ ) {
if(guess.equals(wordContainer[j])) {
wordHiddenContainer[j] = guess;
}
}
}
}