显示二维数组并计算和
代码如下:
package ClassDemo;
public class sumMatrixTest {
public static void main(String[] args) {
int[][] i = new int[][]{{1, 2, 3},{4},{5, 6, 7, 8}};
int sum = sumMatrix(i);
for (int k = 0; k < i.length; k++) {
for (int j = 0; j < i[k].length; j++) {
System.out.print(i[k][j] + " ");
}
System.out.print("
");
}
System.out.println(sum);
}
public static int sumMatrix(int[][] matrix) {
int result = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
result = result + matrix[i][j];
}
}
return result;
}
}