阵列初始化为0到20之间的随机数。 如果该数组中的任何值为0,则此方法应将该行和列中的所有值更改为零。
相反,它会清除数组并将所有值更改为零。 有什么想法吗?
for (int row = 0; row < size; row++){
for (int col = 0; col < size; col++){
if (matrix[row][col] == 0){
for (int k = 0; k < size; k++)
matrix[k][col] = 0;
for (int l = 0; l < size; l++)
matrix[row][l] = 0;
}
}
}
System.out.println("Result:");
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++)
System.out.printf("[%1$2s]", copy[i][j]);
System.out.println();
}
答案 0 :(得分:0)
对于仍在乎的人,这里是完整的代码
Random random = new Random();
int[][] matrix = new int[size][size];
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++)
matrix[i][j] = random.nextInt(20);
}
for (int row = 0; row < size; row++){
for (int col = 0; col < size; col++){
if (matrix[row][col] == 0){
for (int k = 0; k < size; k++)
matrix[k][col] = 0;
for (int l = 0; l < size; l++)
matrix[row][l] = 0;
}
}
}
System.out.println("Result:");
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++)
System.out.printf("[%1$2s]", matrix[i][j]);
System.out.println();
}