这是计算给定矩阵是否为幻方的子代码。我只是对重复的 test[element -1] 感到困惑并且有两个不同的返回。
public static boolean testNormal(int[][] matrix, int dim){
int magicConstant = dim * (dim * dim +1) / 2;
// checks if all the numbers are present
// the default value is false
boolean[] test = new boolean[dim*dim];
int max = dim*dim;
int element;
for (int row = 0; row < dim; row++){
for (int col = 0; col < dim; col++){
element = matrix[row][col];
if ((element > max)|| (element <= 0))
return false;
if (test[element -1])
return false;
test[element -1] = true;
}
}