0
我有一行和一列的總和,但我期望單獨找到每行和列的總和。例如,輸出將是「第1行的總和是..排2總和..等等也是一樣的列以及獲取二維數組中每個單獨行和列的總和
public class TwoD {
public static void main(String[] args) {
int[][] myArray = { {7, 2, 10, 4, 3},
{14, 3, 5, 9, 16},
{99, 12, 37, 4, 2},
{8, 9, 10, 11, 12},
{13, 14, 15, 16, 17}
};
int row = 0;
int col;
int rowSum = 0;
int colSum = 0;
for(col = 0; col<5;col++)
rowSum = rowSum + myArray[row][col];
for(row = 0; row<5; row++)
System.out.println("Sum of row " + row + " is " + rowSum);
col = 0;
for(row=0; row<5;row++)
colSum = colSum + myArray[row][col];
for(col = 0; col<5; col++)
System.out.println("Sum of column " + col + " is " + colSum);
}
}
這輸出每行和列,但我得到相同的總和爲每個行/列輸出 – user3247712 2014-11-24 09:26:26