集册 Java实例教程 计算双数组的平均值

计算双数组的平均值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

422
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
计算双精度数组的平均值

/*
N  o w  J a v a . c o m 提供
*/

//package com.nowjava;


public class Main {

    public static void main(String[] argv) throws Exception {

        double[] v = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,

                37.1234, 67.2344, 68.34534, 69.87700 };

        System.out.println(mean(v));

    }


    /**

     * Computes the mean.

     */

    public static double mean(double[] v) {

        if (v.length == 0)

            throw new IllegalArgumentException(

                    "Nothing to compute! The array must have at least one element.");

        return (mass(v) / (double) v.length);

    }/*来自 时代Java*/


    /**

     * Computes the mean.

     */

    public static double mean(int[] v) {

        if (v.length == 0)

            throw new IllegalArgumentException(

                    "Nothing to compute! The array must have at least one element.");

        return (mass(v) / (double) v.length);

    }


    /**

     * Returns the sum of the elements of the array.

     */

    public static double mass(double[] v) {

        double somme = 0.0;

        for (int k = 0; k < v.length; k++) {

            somme += v[k];

        }

        return (somme);

    }


    
展开阅读全文