我正在尝试编写一个将在矩阵(二维数组)上使用高斯消除的方法,并且我正在尝试调试我的方法并且我遇到了这个问题
public int Gauss() {
int i = 1;
int j = 1;
int pivotCol = 0;
while (pivotCol == 0 && j <= cols())
if (i == rows()){
j ++;
i = 1;
}
if (get(i,j) == 1.0){
pivotCol = j;
} else {
i ++;
}
return pivotCol;
}
这不是最后的方法,但出于某种原因,这个循环永远不会停止,为什么?
答案 0 :(得分:11)
while (pivotCol == 0 && j <= cols()) {
...
}
你忘了括号,所以while只用if语句,因此运行无限。
答案 1 :(得分:7)
我想问题是你的while循环没有花括号,例如它实际上如下:
while (pivotCol == 0 && j <= cols()) {
if (i == rows()){
j++;
i = 1;
}
}
如果i != rows()
这永远不会终止。