我試圖將2d數組中每行的最大值更改爲0.我遇到的問題是它將最高值更改爲零,但它也將最高值之後的值更改爲零。這裏是我的代碼:java 2d數組將每行的最高值更改爲零
public class Test2 {
public static double[][] array1 = //array to be used
{{7.51, 9.57, 6.28, 5.29, 8.7},
{8.07, 6.54, 5.44, 8.78, 8.66},
{9.34, 9.73, 7.19, 6.87, 6.48}};
public static void main(String[] args) {
double h = array1[0][0];// highest value
for (int a = 0; a < array1.length; a++){ //loop through array and change highest values in each row to zero
h = array1[a][0];
for(int b = 0; b < array1[a].length; b++) {
if (array1[a][b] > h){
array1[a][b] = 0;
h = array1[a][b];
}
}
}
System.out.println(); //print array with highest values in each row now changed to zero
for (int x = 0; x < array1.length; x++){
System.out.println();
for (int y = 0; y < array1[x].length; y++){
System.out.print(array1[x][y] + " ");
}
}
}
}
的電流輸出是這樣的:
7.51 0.0 0.0 0.0 0.0
8.07 6.54 5.44 0.0 0.0
9.34 0.0 0.0 0.0 0.0
雖然所需的輸出是這樣的:
7.51 0.0 6.28 5.29 8.7
8.07 6.54 5.44 0.0 8.66
9.34 0.0 7.19 6.87 6.48
我怎樣才能使它只改變最大值,而不是其他?