gpt4 book ai didi

java - ArrayIndexOutOfBoundsException : -1

转载 作者:行者123 更新时间:2023-12-01 12:13:58 25 4
gpt4 key购买 nike

所以我正在编写一个带有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com