【发布时间】:2014-03-05 16:43:58
【问题描述】:
/*
* Programmer: Olawale Onafowokan
* Date: February 6, 2014
* Purpose: Prints the row and column averages
*/
class Lab4 {
public static void main(String[] args) {
int [][] scores = {{ 20, 18, 23, 20, 16 },
{ 30, 20, 18, 21, 20 },
{ 16, 19, 16, 53, 24 },
{ 25, 24, 22, 24, 25 }};
outputArray(scores);
}
public static void outputArray(int[][] array) {
int sum= 0;
int rowSize = array.length;
int columnSize = array[0].length;
System.out.println("rows=" + rowSize + "cols=" + columnSize);
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
sum += array[i][j];
}
System.out.println("Print the sum of rows = " + sum);
}
for (int i = 0; i < array.length; i++) {
sum = 0;
sum = sum + array[i][j];
// It is telling me the j can't be resolved
}
}
}
程序打印出来:
rows=4cols=5
Print the sum of rows = 612
Print the sum of rows = 20358
Print the sum of rows = 652058
Print the sum of rows = 20866609
我不明白为什么它没有正确地将数字相加。我正在尝试将每一行和每一列相加。我什至不确定这些数字是从哪里来的。
【问题讨论】:
标签: java arrays row multiple-columns