gpt4 book ai didi

java - 绞刑吏游戏中的 ArrayIndexOutOfBoundsException 错误

转载 作者:行者123 更新时间:2023-12-01 21:58:59 26 4
gpt4 key购买 nike

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

按照我的方法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 {...

关于java - 绞刑吏游戏中的 ArrayIndexOutOfBoundsException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58717390/

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