java - ArrayIndexOutOfBoundsException : -1

标签 java arrays indexoutofboundsexception

所以我正在编写一个带有 2D 数组的代码,将其排列成一个方 table ,比如 10 x 10。它充满了 X、O 和空格。输入阈值,如果 X 或 O 周围的索引的百分比也大于阈值,则该点满足,否则不满足。我已经能够很好地打印表格,但后来我尝试做令人满意的部分,但我得到了数组索引越界异常。我知道这意味着什么,但不确定如何让我的代码正常工作,尽管我担心我必须重新设计它。另一件事是,我不确定我的 boolean 运算是否正确。

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;
}



}

最佳答案

您应该检查row, col是否>=0且total++ 并一起减少条件来重构 isSatisfied 方法中的代码。考虑在此处使用正则表达式。

关于java - ArrayIndexOutOfBoundsException : -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27116016/

相关文章:

java - Swing,远程桌面异常

java - 如何在 Spring XML 配置中设置 LocalDateTime

java - Hibernate 查询没有检索结果?

c - 在c中为结构体中的数组赋值

C++ 二维字符数组到字符串

java - 无法找到java.lang.ArrayIndexOutOfBoundsException的原因

c# - 转换 DateTime 导致 IndexOutOfRangeException

java - Java 8 中的 Lambda 表达式

java - 线程不同时运行

arrays - Excel VBA 编译错误参数在遍历附加到类集合的数组时不是可选的