【发布时间】:2023-04-02 22:50:01
【问题描述】:
n 在其循环结束时等于 2,但在最后一个“if”语句中它以某种方式为 3。)
无法弄清楚它在哪里改变。
因为最后一个 if 条件没有得到满足,所以 for 循环继续运行而不是返回 true (for(int i=0;i<Board[i].length;i++)) 和 ) 得到一个越界异常。
所以两个问题-
- 你能看出 n 的变化吗?
- 为什么我的for循环在满足条件的情况下仍然继续?
Board[i].length=3并且循环继续运行并给我一个越界异常,而不是退出 for 循环并在它之后返回 false。
public boolean ColChecker() {
int n=0;
// create boolean array and set all values to false
boolean[] isExist = new boolean[10];
for(int i=0;i<isExist.length;i++)
isExist[i]=false;
//loop over columns and test using whosThereCol method
for(int i=0;i<Board[i].length;i++) {
for(int col=0;col<Board[0][0].getLength();col++) {
for(n=0;n<Board.length;n++)
Board[n][i].whosThereCol(col,isExist);
//if array still has missing values, column incomplete - return false
for(int j=1;j<10;j++)
if(!isExist[j])
return false;
//if no missing values, initialize array to false values for next iteration of for loop
for(int j=1;j<10;j++)
isExist[j]=false;
// "if" statement checks if this is the last column in the last square, if so, we passed all the tests. return true
if(i+1==Board[0].length&&col+1==Board[0][0].getLength()&&n+1==Board.length)
return true;
}
}
return false;
}
【问题讨论】: