错误行是 13。错误是 Array index out of bound。 http://pastebin.com/L3FtzarH
该方法是在字符串中查找一个字符,它可以正常工作,除非在同一字符旁边有重复字符,例如在单词“Rabbit”中给我一个错误的字母“b”,因为它是两次并在同一个字母旁边。
我在做刽子手游戏。对于空格,我将索引乘以 to。例如:如果秘密词是“猫”那么它会出现3个下划线:“_ _ _”那个下划线字符串有6个字符,所以如果字母是A(世界“猫”中的索引1)我乘以2填充字符串下划线的正确位置:“_ A _”
编辑 1,您的代码:
public void buscarLetra() {
chequearif = false;
string = "";
letra = letraEscogida.toCharArray();
System.out.println("la letra pasa a char:" + letra[0]);
int index = 0;
for (int i = 0; i < (palabraElegida.length()); i++) {
if (palabraElegida.substring(i, i + 1).equals(letraEscogida) == true) {
if (chequearif == true) {
string = "";
index = palabraElegida.indexOf(letraEscogida, index + 1);
index *= 2;
arrayGuiones[index] = letra[0];
System.out.println("segundo index: " + index);
index = 0;
}
index = palabraElegida.indexOf(letraEscogida);
index *= 2;
System.out.println("primer index: " + index);
arrayGuiones[index] = letra[0];
for (int j = 0; j < arrayGuiones.length; j++) {
string += arrayGuiones[j] + "";
}
lbl_palabra.setText(string);
chequearif = true;
}
}
}