我差不多完成了一个基本的Hangman游戏,但我在课堂上遇到了“makeGuess”方法的困难。我正在尝试针对秘密词测试用户输入(他们的一个字符猜测),并且如果他们的猜测正确或不正确则更新伪装的单词。任何有关正确方向的建议或指示都将受到赞赏。我每次跑步都会收到这个错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 11
at java.lang.String.charAt(String.java:686)
at hangmandemo.Hangman.makeGuess(Hangman.java:42)
at hangmandemo.HangmanDemo.main(HangmanDemo.java:24)
Java Result: 1
这是Hangman类:
package hangmandemo;
public class Hangman {
private String secretWord;
private String disguisedWord;
private int guesses;
private int wrongGuesses;
boolean found=false;
public String getDisguisedWord() {
return disguisedWord;
}
public String getSecretWord() {
return secretWord;
}
/**
* returns number of guesses
*/
public int getGuesses() {
return guesses;
}
/**
* continues game until secretWord is found
*/
public boolean isFound() {
return found;
}
/**
* brings in a char to test against String
*/
public void makeGuess(char c) {
for (int i = 0; i <= disguisedWord.length(); i++) {
if (c == secretWord.charAt(i)) {
disguisedWord = disguisedWord.substring(0, i-1) + c + disguisedWord.substring(i+1);
}
if (secretWord.equals(disguisedWord)) {
found = true;
}
}
wrongGuesses++;
}
/**
* assigns secret String to secret word
*/
public void createSecretWord(String secret) {
secretWord = secret;
}
/**
* disguises secret word with "???"
*/
public void createDisguisedWord() {
for (int i = 0; i < secretWord.length(); i++) {
disguisedWord += "*";
}
}
/**
* returns number of wrong guesses
*/
public int getWrongGuesses() {
return wrongGuesses;
}
}
这是我的演示主类
package hangmandemo;
import java.util.Scanner;
public class HangmanDemo {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Hangman game = new Hangman();
game.createSecretWord("assessments");
game.createDisguisedWord();
System.out.println("This is the hangmandemo. Press the number '3' to quit.");
System.out.println("The disguised word is..."+ game.getDisguisedWord());
while (!game.isFound()) {
System.out.println("Enter a one character guess");
char c = keyboard.next().charAt(0);
if (c == 3) {
System.out.println("game over, the word was... " + game.getSecretWord());
System.exit(0);
}
game.makeGuess(c);
System.out.println(game.getDisguisedWord());
}
System.out.println("you guessed it, the word was... " + game.getSecretWord());
System.out.println("you had " + game.getGuesses() + " guesses.");
System.out.println("you had " + game.getWrongGuesses() + " wrong guesses.");
}
}
答案 0 :(得分:1)
这里有3个问题。
循环中的
for (int i = 0; i <= disguisedWord.length(); i++)
disguisedWord
的最后一个元素是'伪装的字[disguisedWord.length -1],因为数组从0开始索引。
习惯上将循环格式化为
for (int i = 0; i < disguisedWord.length(); i++)
但即使这还不够,因为
disguisedWord.substring(i+1)
在那时会超出界限。
和表达式
disguisedWord.substring(0, i-1)
当i = 0时,会抛出异常;
你需要在这里密切关注你的边缘条件。
答案 1 :(得分:0)
int i = 0; i <= disguisedWord.length(); i++
这一行,您必须将<=
更改为<
否则您将访问索引,无论字符串的长度是多于最大索引的1,因为事实字符串索引从0开始,长度只是字符总数
答案 2 :(得分:0)
替换for (int i = 0; i <= disguisedWord.length(); i++)
for (int i = 0; i < disguisedWord.length(); i++)
。
Everthing 在JAVA中以0为基础编入索引:"abc"
在'a'
处有0
字符'b'
,在{{1}处有1
}和'c'
位于2
,而其长度为3
。