2014-11-24 92 views
0

因此,我正在编写一个2D数组代码,将它排列成一个方形表格,例如10乘10。它填充了X和O以及空格。输入一个阈值,如果X或O周围的索引的百分比也是Xs或Os大于阈值,则该点满足,否则不满意。我已经能够很好地打印表格了,但是我试图做出令人满意的部分,并且我得到了一个数组索引越界异常。我对这意味着什么有了一个想法,但不知道如何让我的代码正常工作,但我担心我将不得不重新设计它。另一件事是,我不确定我是否正确布尔值。ArrayIndexOutOfBoundsException:-1

public class CellSim{ 

public static void main(String[] args){ 
    char [][] tissue = new char [10][10]; 
    int threshold = 30; 
    assignCellTypes(tissue, 50, 25); 
    printTissue(tissue); 
    System.out.println(); 

    boolean boardSat = true; 
    boardSat = boardSatisfied(tissue, threshold); 

    if(boardSat == false){ 
     System.out.println("board is not satisfied");} 
    if(boardSat == true){ 
     System.out.println("board is satisfied");} 



} 


public static void printTissue(char[][] tissue){ 
    for(int row = 0;row < tissue.length;row++){ 
     for(int col = 0;col < tissue[row].length;col++){ 
      System.out.print(tissue[row][col] + "\t"); 
     } 
     System.out.println(); 
    } 
} 


public static void assignCellTypes(char[][] tissue, int percentBlank, int percentX){ 
int n = (tissue.length) * (tissue.length); 
percentBlank = (int) Math.ceil(n * (percentBlank * .01)); 
percentX = (int) Math.ceil((n - percentBlank) * (percentX * .01)); 
int percentO = (int) Math.ceil(n - percentBlank - percentX); 

for(int i = 0; i < percentBlank; i++){ 
while(percentBlank > 0){ 
    int randCell = randInt(0, 9); 
    int randCell2 = randInt(0, 9); 
       if(tissue[randCell][randCell2] == '\u0000'){ 
        tissue[randCell][randCell2] = ' '; 
        break; 
        } 
} 
} 
for(int i = 0; i < percentX; i++){ 
while(percentX > 0){ 
    int randCell = randInt(0, 9); 
    int randCell2 = randInt(0, 9); 
       if(tissue[randCell][randCell2] == '\u0000'){ 
        tissue[randCell][randCell2] = 'X'; 
        break; 
        } 
} 
} 
for(int i = 0; i < percentO; i++){ 
while(percentO > 0){ 
    int randCell = randInt(0, 9); 
    int randCell2 = randInt(0, 9); 
       if(tissue[randCell][randCell2] == '\u0000'){ 
        tissue[randCell][randCell2] = 'O'; 
        break; 
        } 
} 
} 

} 
public static boolean isSatisfied(char[][] tissue, int row, int col, int threshold){ 
    int total = 0; 
    int same = 0; 
    if(tissue[row][col] == 'X'){ 
     total = 0; 
      if(tissue[row + 1][col - 1] == 'X'){ 
       same ++; 
       total ++; 
      }else if(tissue[row + 1][col - 1] == 'O') 
       total ++; 
      if(tissue[row + 1][col] == 'X'){ 
       same ++; 
       total ++; 
      }else if(tissue[row + 1][col] == 'O') 
       total ++; 
      if(tissue[row + 1][col + 1] == 'X'){ 
       same ++; 
       total ++; 
      }else if(tissue[row + 1][col + 1] == 'O') 
       total ++; 
      if(tissue[row][col - 1] == 'X'){ 
       same ++; 
       total ++; 
      }else if(tissue[row][col - 1] == 'O') 
       total ++; 
      if(tissue[row][col + 1] == 'X'){ 
       same ++; 
       total ++; 
      }else if(tissue[row][col + 1] == 'O') 
       total ++; 
      if(tissue[row - 1][col - 1] == 'X'){ 
       same ++; 
       total ++; 
      }else if(tissue[row - 1][col - 1] == 'O') 
       total ++; 
      if(tissue[row - 1][col] == 'X'){ 
       same ++; 
       total ++; 
      }else if(tissue[row - 1][col] == 'O') 
       total ++; 
      if(tissue[row - 1][col + 1] == 'X'){ 
       same ++; 
       total ++; 
      }else if(tissue[row - 1][col + 1] == 'O') 
       total ++; 

    } 
    if(tissue[row][col] == 'O'){ 
     total = 0; 
      if(tissue[row + 1][col - 1] == 'O'){ 
       same ++; 
       total ++; 
      }else if(tissue[row + 1][col - 1] == 'X') 
       total ++; 
      if(tissue[row + 1][col] == 'O'){ 
       same ++; 
       total ++; 
      }else if(tissue[row + 1][col] == 'X') 
       total ++; 
      if(tissue[row + 1][col + 1] == 'O'){ 
       same ++; 
       total ++; 
      }else if(tissue[row + 1][col + 1] == 'X') 
       total ++; 
      if(tissue[row][col - 1] == 'O'){ 
       same ++; 
       total ++; 
      }else if(tissue[row][col - 1] == 'X') 
       total ++; 
      if(tissue[row][col + 1] == 'O'){ 
       same ++; 
       total ++; 
      }else if(tissue[row][col + 1] == 'X') 
       total ++; 
      if(tissue[row - 1][col - 1] == 'O'){ 
       same ++; 
       total ++; 
      }else if(tissue[row - 1][col - 1] == 'X') 
       total ++; 
      if(tissue[row - 1][col] == 'O'){ 
       same ++; 
       total ++; 
      }else if(tissue[row - 1][col] == 'X') 
       total ++; 
      if(tissue[row - 1][col + 1] == 'O'){ 
       same ++; 
       total ++; 
      }else if(tissue[row - 1][col + 1] == 'X') 
       total ++; 


    } 
    if(tissue[row][col] == ' '){ 
    return true; 
    }if(total == 0){ 
     return false; 
     }else if(((same/total) * 100) >= threshold){ 
     return true; 
    }else{ return false;} 
}  

public static boolean boardSatisfied(char[][] tissue, int threshold){ 
    boolean isSat = true; 
    while(isSat == true){ 
     for(int row = 0;row < tissue.length;row++){ 
      for(int col = 0;col < tissue[row].length;col++){ 
      isSat = isSatisfied(tissue, row, col, threshold); 

      } 
     } 
    } 
     if(isSat == false){ 
     return false; 
     }else{return true;} 
} 


public static int randInt(int min, int max){ 

    int range = (max - min) + 1;  
    return(int)(Math.random() * range) + min; 
} 



} 
+0

异常告诉你发生错误的文件和行号。我建议在调试器中逐步完成。只要你有真实的数据告诉你发生了什么,这很容易解决。 – duffymo 2014-11-24 23:19:49

+9

今天是学习[如何调试]的好日子(http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 – 2014-11-24 23:23:39

+0

“我对这意味着什么有一个想法_”这意味着你试图访问一个不存在的数组元素。例如,如果您有一个长度为5(最后一个索引为4)的数组,则不能访问元素10,因为它不存在。 – csmckelvey 2014-11-24 23:25:26

回答

0

问题在于你的isSisisfied方法。您不执行任何边界检查以确保您只能访问阵列中的有效选项。你不应该尝试访问数组边界之外的值。当您这样做时,您会收到一个ArrayIndexOutOfBounds异常。这就是你的情况。正如您对问题的评论所建议的,您应该进一步调查边界检查,以便您了解此处的根本问题。

你的方法应该改成类似:

public static boolean isSatisfied(char[][] tissue, int row, int col, int threshold){ 
int total = 0; 
int same = 0; 
if(tissue[row][col] == 'X'){ 
    total = 0; 
     if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'X'){ 
      same ++; 
      total ++; 
     }else if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'O') 
      total ++; 
     if(row+1 < tissue.length && tissue[row + 1][col] == 'X'){ 
      same ++; 
      total ++; 
     }else if(row+1 < tissue.length && tissue[row + 1][col] == 'O') 
      total ++; 
     if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'X'){ 
      same ++; 
      total ++; 
     }else if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'O') 
      total ++; 
     if(col-1 >= 0 && tissue[row][col - 1] == 'X'){ 
      same ++; 
      total ++; 
     }else if(col-1 >= 0 && tissue[row][col - 1] == 'O') 
      total ++; 
     if(col+1 < tissue[row].length && tissue[row][col + 1] == 'X'){ 
      same ++; 
      total ++; 
     }else if(col+1 < tissue[row].length && tissue[row][col + 1] == 'O') 
      total ++; 
     if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'X'){ 
      same ++; 
      total ++; 
     }else if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'O') 
      total ++; 
     if(row-1 >= 0 && tissue[row - 1][col] == 'X'){ 
      same ++; 
      total ++; 
     }else if(row-1 >= 0 && tissue[row - 1][col] == 'O') 
      total ++; 
     if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'X'){ 
      same ++; 
      total ++; 
     }else if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'O') 
      total ++; 

} 
if(tissue[row][col] == 'O'){ 
    total = 0; 
     if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'O'){ 
      same ++; 
      total ++; 
     }else if(col-1 >= 0 && row+1 < tissue.length && tissue[row + 1][col - 1] == 'X') 
      total ++; 
     if(row+1 < tissue.length && tissue[row + 1][col] == 'O'){ 
      same ++; 
      total ++; 
     }else if(row+1 < tissue.length && tissue[row + 1][col] == 'X') 
      total ++; 
     if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'O'){ 
      same ++; 
      total ++; 
     }else if(row+1 < tissue.length && col+1 < tissue[row+1].length && tissue[row + 1][col + 1] == 'X') 
      total ++; 
     if(col-1 >= 0 && tissue[row][col - 1] == 'O'){ 
      same ++; 
      total ++; 
     }else if(col-1 >= 0 && tissue[row][col - 1] == 'X') 
      total ++; 
     if(col+1 < tissue[row].length && tissue[row][col + 1] == 'O'){ 
      same ++; 
      total ++; 
     }else if(col+1 < tissue[row].length && tissue[row][col + 1] == 'X') 
      total ++; 
     if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'O'){ 
      same ++; 
      total ++; 
     }else if(row-1 >= 0 && col-1 >= 0 && tissue[row - 1][col - 1] == 'X') 
      total ++; 
     if(row-1 >= 0 && tissue[row - 1][col] == 'O'){ 
      same ++; 
      total ++; 
     }else if(row-1 >= 0 && tissue[row - 1][col] == 'X') 
      total ++; 
     if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'O'){ 
      same ++; 
      total ++; 
     }else if(row-1 >= 0 && col+1 < tissue[row-1].length && tissue[row - 1][col + 1] == 'X') 
      total ++; 


} 
if(tissue[row][col] == ' '){ 
return true; 
}if(total == 0){ 
    return false; 
    }else if(((same/total) * 100) >= threshold){ 
    return true; 
}else{ return false;} 
}  
1

您应该检查row, col为> = 0和< tissue.size。行/列+/- 1相同。而且,您也可以通过移出total++并一起降低条件来重构isSatisfied方法中的代码。考虑在这里使用正则表达式。

相关问题