public static int berekenSomElementenIdentiekRijKolom(int[][] a) {
int som = 0;
// for (int i = 0; i < a.length; i++) {
// for (int j = 0; j < a.length; j++) {
// if ( i == j ) {
// som += a[i][j];
// }
// }
//
// }
// return som;
// }
for (int[] rij : a){
for (int[] kol :a){
if (rij == kol){
som+= int[rij][kol];
}
}
}
return som;
}
这是我正在编写的代码。如果行索引等于列索引,则目标很简单。正常的循环可以工作,但我在将其转换为增强循环时遇到了问题。我听说它有时不起作用,我接受它。
https://stackoverflow.com/questions/51832016
复制相似问题