我的程序在这段代码中抛出StringIndexOutOfBoundsException:temp1 = temp.replace('-', temp.charAt(p));
我正在尝试获取相同字母的索引(在比较输入的字母和单词之后)并删除“ - ”以显示用户具有猜对了。** **我一直试着几个小时无济于事。我认为问题出在我的循环中。谢谢你的答案:)如果我违反任何规定,请原谅我。
run: ----- Enter a letter: a Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range:
3 at java.lang.String.charAt(String.java:658) 在Hangman.main(Hangman.java:34) Java结果:1
import java.util.Scanner;
public class Hangman {
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
String word = "Sample";
String temp = null;
String temp1 = null;
String letter = null;
int n;
int m=0;
int p = 0;
for (n = 0; n<word.length(); n++){
temp = word.replaceAll(word, "-"); //replaces the String word with "-" and prints
System.out.print(temp);
}
while (m<=5){ //the player can only guess incorrectly 5 times
System.out.println("\nEnter a letter:");
letter = sc.nextLine();
letter.toLowerCase();
if (word.contains(letter) == true){
p = word.indexOf(letter);
temp1 = temp.replace('-', temp.charAt(p)); //if the word contains the letter, "-" is replaced by the letter.
System.out.print(temp1);
}
else {
System.out.print("\nMissed: "+letter); //if not, Missed: +the given letter
m++; //to count for incorrect guesses
}
System.out.print(temp1);
}
System.out.println("Game Over.");
}
}
答案 0 :(得分:2)
执行此操作时:
temp = word.replaceAll(word, "-");
...您将temp
设置为“ - ”,而不是(例如)“----”。要了解原因,请考虑word
是否为“你好”;然后这一行看起来像:
temp = "hello".replaceAll("hello", "-");
所以,稍后您假设temp
与word
一样长,因为您在word
中找到了一个索引并尝试在temp
中访问该字符。但temp
只有一个字符长,因此例外。
p = word.indexOf(letter);
temp1 = temp.replace('-', temp.charAt(p));
答案 1 :(得分:0)
if (word.contains(letter) == true){
p = word.indexOf(letter);
temp1 = temp.replace('-', temp.charAt(p)); //if the word contains the letter, "-" is replaced by the letter.
System.out.print(temp1);
}
word.indexOf(letter);如果后者出现在字符串中则返回字母索引,否则为-1。这就是你得到例外的原因。
答案 2 :(得分:0)
你应该检查replaceAll()方法的文档。因为你错了。
replaceAll(String regex,String replacement) 将与给定正则表达式匹配的此字符串的每个子字符串替换为给定的替换。
您将整个字符串放入正则表达式参数
如果你myString.replaceAll("\\.","-");
(使用双反斜杠来指定正则表达式)将替换换行符旁边的任何字符,并使用“ - ”检查正则表达式。 Regullar expressions
答案 3 :(得分:0)
试试这个...... 这将解决您的问题...... !!
包豆; import java.util.Scanner;
公共班Hangman {
public static String replace(String str, int index, char replace){
if(str==null){
return str;
}else if(index<0 || index>=str.length()){
return str;
}
char[] chars = str.toCharArray();
chars[index] = replace;
return String.valueOf(chars);
}
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
String word = "Sample";
String temp = "";
String letter = null;
int n;
int m=0;
int p = 0;
for (n = 0; n<word.length(); n++){
temp = temp + word.replaceAll(word, "-"); //replaces the String word with "-" and prints
}
System.out.print(temp);
while (m <= 5){ //the player can only guess incorrectly 5 times
System.out.println("\nEnter a letter:");
letter = sc.nextLine();
letter.toLowerCase();
if (word.contains(letter) == true){
p = word.indexOf(letter);
temp = replace(temp, p , word.charAt(p)); //if the word contains the letter, "-" is replaced by the letter.
System.out.println(temp);
}
else {
System.out.print("\nMissed: "+letter); //if not, Missed: +the given letter
m++; //to count for incorrect guesses
}
}
System.out.println("Game Over.");
}
}