计算多维数组的平均值

时间:2016-10-13 03:00:34

标签: java arrays multidimensional-array

我被要求计算多维数组(表)的平均值。我认为我正确地构建了一切(至少我觉得如此):

import java.util.Scanner;

public class Exam {
    static int NROWS = 5;
    static int NCOL = 6;
    static int[][] data = new int[NROWS][NCOL];

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    enterData(scan);
    countAvg();
}

static void enterData(Scanner in) {
    System.out.println("How many rows ? (up to " + NROWS + ")");
    int rows = in.nextInt();
    System.out.println("How many cols ? (up to " + NCOL + ")");
    int cols = in.nextInt();
    for (int i = 0; i < rows; i++) {
        System.out.println("Row" + i + ": ");
        for (int j = 0; j < cols; j++) {
            System.out.print("Enter Data for Column " + j + ":");
            data[i][j] = in.nextInt();
        }
    }
}

static void countAvg() {
    int total = 0;
    int count = 0;
    for (int i = 0; i < NROWS; i++) {
        for (int j = 0; j < NCOL; j++) {
            total = total + data[i][j];
            count = count++;
        }
    }
    int avg = total / count;
    System.out.println("the average is: " + avg);
}

}

但系统仍然向我推送这条消息:

  

线程“main”中的异常java.lang.ArithmeticException:/ by zero       在Exam.countAvg(Exam.java:33)       在Exam.main(Exam.java:9)

请帮助我理解我做错了什么。非常感谢!

3 个答案:

答案 0 :(得分:0)

正如Michael Pickett在上面的评论中所提到的,问题与count = count++;有关。因为你正在使用post-increment,所以你的计数变为零的所有方式,所以在执行行之后会发生递增。如果你使用pre-increment count = ++count;在执行行时会发生递增。

因此,您可以使用上述pre-incrementpost-increment而不将其分配给count变量。

答案 1 :(得分:0)

首先,将count = count++;更改为count++;

其次,您正在进行整数除法,因此将int avg = total/count;更改为double int = total / (double) count;

第三,你将数据数组设置为5X6,即使它们输入了不同的数据,这也会减少平均值,因为每个单元格的数量都会增加30个。

相反,试试这个

import java.util.Scanner;

public class Exam {
    static int[][] data;
    private static int rows = 0;
    private static int cols = 0;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        enterData(scan);
        countAvg();
    }

    static void enterData(Scanner in) {
        System.out.println("How many rows ?");
        rows = in.nextInt();
        System.out.println("How many cols ?");
        cols = in.nextInt();
        data = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            System.out.println("Row" + i + ": ");
            for (int j = 0; j < cols; j++) {
                System.out.print("Enter Data for Column " + j + ":");
                data[i][j] = in.nextInt();
            }
        }
    }

    static void countAvg() {
        int total = 0;
        int count = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                total = total + data[i][j];
                count++;
            }
        }
        double avg = total / (double) count;
        System.out.println(count + "the average is: " + avg);
    }
}

这将允许您使用输入行和列创建新数据数组,这样您就可以正确计算平均值。

答案 2 :(得分:0)

除了其他答案(两者都正确)之外,还要注意一些可能会提高代码可读性的方法。你可以使用'for-each'循环而不是使用索引,你可以使用+=将值添加到总数中:

for (int[] row: data) {
    for (int value: row) {
        total += value;
        count++;
    }
}

另请注意,如果您熟悉Java 8,则可以在一个语句中执行此操作:

Arrays.stream(data).flatMapToInt(Arrays::stream).average()

返回OptionalDouble以允许数组为空(通过返回Optional.empty