这是我写的代码,用于检查字符串数组中的字符串,并替换保存在另一个字符串数组中的相同位置的字符。在字符串中查找字符
char[][] currentGuessArray = new char[currentGuessPhrase.length][];
for (int x = 0; x < currentGuessPhrase.length; x++) {
currentGuessArray[x] = currentGuessPhrase[x].toCharArray();
}
for (int x = 0; x < correctPhrase.length; x++) {
for (int a = 0; a < correctPhrase[x].length(); a++) {
if (correctPhrase[x].charAt(a) == guess) {
currentGuessArray[x][a] = guess;
}
}
}
//Convert chars back to string array
for (int x = 0; x < currentGuessArray[x].length - 1; x++){
currentGuessPhrase[x] = new String(currentGuessArray[x]);
}
有关示例运行:这些是currentPhrase
(5项)的值:
sunny banana noun trained named
变量guess:
guess = "a"
预期输出:
_____ _a___a ____ __a____ _a___
实际输出T:
_____ ______ ____ _______ _____
此外,for (int x = 0; x < currentGuessArray[x].length - 1; x++){
抛出:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at a.updateGuess(a.java:97)
at a.main(a.java:110)
任何人谁可以与任何的问题,帮助将不胜感激,这不是功课。
您可以创建[SSCCE?](http://sscce.org/) – Smit
@vandale x是字符串,a是字符位置。 –
String.indexOf(char)将查找字符串中字符的位置。还有另一个版本将从给定的偏移量开始。 –