在单独的方法中平均数组

时间:2014-04-08 06:44:08

标签: java

我的原始代码是:

import javax.swing.JOptionPane;

public class average {

    public static void main(String[] args) {        
        String str1;
        String str2;
        String str3;
        int    numbersToAverage;

        str1 = JOptionPane.showInputDialog("Please enter the amount of numbers you would like to average: ");
        numbersToAverage = Integer.parseInt(str1);

        // while statement will provide error message so user cannot enter 0
        while (numbersToAverage < 1) {
            str3 = JOptionPane.showInputDialog("Invalid entry: Please enter one or more numbers to average: ");
            numbersToAverage = Integer.parseInt(str3);
        }

        // array size is set to equal user input
        double[] numbers = new double[numbersToAverage];
        double sum = 0;

        for (int i = 0; i < numbersToAverage; i++) {
            str2       = JOptionPane.showInputDialog("Enter number " + (i + 1)
            numbers[i] = Double.parseDouble(str2);

            // the sum equals numbers in the array added together
            sum += numbers[i];
        }

        // Calculates the average of the numbers entered by the user
        double average = sum / numbersToAverage;

        // Prints in a dialogue box to user the average
        JOptionPane.showMessageDialog(null, "The average of the numbers you entered is: " + average);
    }
}

我现在必须以单独的方法计算平均值,而我很难弄清楚如何做到这一点?到目前为止,我有:

import javax.swing.JOptionPane;

public class average {
    public static void main(String[] args) {
        String str1;
        String str2;
        String str3;
        int numbersToAverage;

        str1 = JOptionPane
            .showInputDialog("Please enter the amount of numbers you would like to average: ");
        numbersToAverage = Integer.parseInt(str1);

        // while statement will provide error message so user cannot enter 0
        while (numbersToAverage < 1) {
            str3 = JOptionPane
                .showInputDialog("Invalid entry: Please enter one or more numbers to average: ");
            numbersToAverage = Integer.parseInt(str3);
        }

        // array size is set to equal user input
        double[] numbers = new double[numbersToAverage];

        //double sum = 0;
        for (int i = 0; i < numbersToAverage; i++) {
            str2       = JOptionPane.showInputDialog("Enter number " + (i + 1)
            numbers[i] = Double.parseDouble(str2);
            JOptionPane.showMessageDialog(null, "The average of the numbers you entered is: " );
        }
    }

    public static double average(double[] array){
        double sum; 

        sum += array[i];
        double average = sum / array.length;

        return average;
    }
}

我很难弄清楚如何在main方法中调用它以及如何获得正确的计算。做这个的最好方式是什么?我是否需要在普通方法中使用for循环,还是应该将其保留在主要方法中?

3 个答案:

答案 0 :(得分:1)

这样做:

StatUtils.mean(array);

您可以找到有关StatUtils here的更多信息。

答案 1 :(得分:0)

在Java 8中,可以使用Streams:

final double avg = array.length > 0 ? DoubleStream.of(array).sum() / array.length : 0;

或者在自己的功能中:

public static double average(final double[] array) {
    return array.length > 0 ? DoubleStream.of(array).sum() / array.length : 0;
}

答案 2 :(得分:-2)

是的,你需要一个循环,尝试做这样的事情:

public static double average(double[] array) {

    if(array.length == 0) {
        return 0;
    }

    double sum = 0;

    for (int i = 0; i < array.length; i++) {
        sum += array[i];
    }

    double average = sum / array.length;
    return average;
}