0

错误行是 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;
         }
      }
   }
4

1 回答 1

1

在您的if (chequearif == true) {块内,您尝试indexindexOf()通话中使用。您试图在您认为index指向的第一次出现之后检查更多的出现,但您忘记了您之前已将 index 乘以 2。因此它指向的位置超出了第二次出现,因此indexOf()返回 -1。你把它加倍到 -2,然后试着说arrayGuiones[index],这显然不会与负索引一起工作。

于 2011-10-08T20:14:12.010 回答