Hangman游戏中的ArrayIndexOutOfBoundsException错误



我是Java的新手,对于我的AP计算机科学课程,我们正在创建一个刽子手游戏。

在我的方法guessCheck中,我有一个增强的for循环,用于检查letterGuess是否与masterWord中的任何字母匹配。如果匹配,则将censoredWord的索引替换为letterGuess。给我带来问题的代码行是censoredWord[x] = letterGuess;。为什么它抛出ArrayIndexOutOfBoundsException错误?

public class Hangman {
static String[] wordList = new String[]{"wait", "release", "important", "mark", "electric", "defective", "poke",
"blue", "beef", "spring", "hurt", "orange", "happy", "zealous", "flowery", "accurate", "brake", "title",
"festive", "wrathful", "scissors", "peaceful", "finicky", "shape", "soothe", "head", "spotted", "needless",
"time", "abundant", "humdrum", "mouth", "trot", "bounce", "thank", "avoid", "shocking", "minor", "secret"};
static int lives = 5;
static String placeholder = wordList[(int) (Math.random() * wordList.length)];
static char[] MasterCopyWord = placeholder.toCharArray();
static String playerName;
static char censoredWord[] = new char[placeholder.length()];
static boolean didIWin = false;
static char[] guessedChars = new char[26];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
initGame();
// game starting screen and info
System.out.println("Enter your name:");
String playerName = sc.nextLine();
System.out.println("Hello " + playerName
+ ", you are playing Hangman where you try to guess a random word. You have 5 lives.");
// Game start text
System.out.println("The game will start! Guess the word by typing a one letter at a time!");
System.out.println("Guess your " + MasterCopyWord.length + " letter word!");
System.out.println(censoredWord);
System.out.print("Cheat: Chosen word is ");
System.out.println(placeholder);
System.out.println("Guessed Letters:" + new String(guessedChars));
// game loop
while (!didIWin) {
char guess = sc.next().charAt(0);
System.out.println("Guessed Letters:" + new String(guessedChars));
System.out.print("Cheat: Chosen word is ");
System.out.println(placeholder);
guessCheck(guess);
System.out.println(censoredWord);
}
}
// check if letterGuess is correct
public static void guessCheck(char letterGuess) {
for (char y : guessedChars) {
if (guessedChars[y] == 'u0000') {
guessedChars[y] = letterGuess;
}
}
//iterates through masterWord, replacing any
//matching letters in censoredWord
for (char x : MasterCopyWord) {
if (x == letterGuess) {
censoredWord[x] = letterGuess;
} else {
lives--;
}
// check for lives
boolean wordMatch = Arrays.equals(MasterCopyWord, censoredWord);
if (lives <= 0) {
System.out.println("You lost!");
System.exit(0);
} else if (wordMatch) {
System.out.println("Congrats " + playerName
+ "! You correctly guessed the word! The game will now terminate");
didIWin = true;
}
}
}
public static void initGame() {
// makes copy of chosen word and censors it
for (int x = 0; x < placeholder.length(); x++) {
censoredWord[x] = '*';
}
}
}

'x'是猜测的字符(类型为char(,不能用作字符串中的索引(这是一个数字而不是字符(。你需要知道索引才能在你的审查词中替换它。也许不是

for(char x: MasterCopyWord)

相反,您应该用数字迭代单词

for(int i = 0; i < MasterCopyWord.length(); i++) {
if( MasterCopyWord[i] == letterGuess) {
censoredWord[i] = letterGuess;
} else {...

最新更新