English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Java

Java 流程控制

Java 数组

Java 面向对象(I)

Java 面向对象(II)

Java 面向对象(III)

Gestion des exceptions Java

Java 列表(List)

Java Queue(队列)

Java Map集合

Java Set集合

Java 输入输出(I/O)

Java Reader/Writer

Java 其他主题

Java程序来计算标准偏差

Comprehensive Java examples

在此程序中,您将学习如何使用Java中的函数来计算标准差。

该程序使用数组计算单个系列的标准偏差。

为了计算标准偏差,将创建函数calculateSD()。包含10个元素的数组传递给该函数,此函数计算标准偏差并将其返回给main()函数。

示例:计算标准偏差的程序

public class StandardDeviation {
    public static void main(String[] args) {
        double[] numArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        double SD = calculateSD(numArray);
        System.out.format("标准偏差 = %.6f", SD);
    }
    public static double calculateSD(double numArray[])
    {
        double sum = 0.0, standardDeviation = 0.0;
        int length = numArray.length;
        for(double num : numArray) {}}
            sum += num;
        }
        double mean = sum/length;
        for(double num: numArray) {
            standardDeviation += Math.pow(num - mean, 2);
        }
        return Math.sqrt(standardDeviation/length);
    }
}

Note:This program will calculate the standard deviation of the sample. If you need to calculate the total number of S.D., from the calculateSD() method return Math.sqrt(standardDeviation/(length-1)) instead of Math.sqrt(standardDeviation/length).

When running this program, the output is:

Standard deviation = 2.872281

In the above program, we usedMath.pow()andMath.sqrt()help to calculate powers and square roots separately.

Comprehensive Java examples