第七章第十一题(统计:计算标准差)(Statistics: calculating standard deviation)

第七章第十一题(统计:计算标准差)(Statistics: calculating standard deviation)

  • *7.11(统计:计算标准差)编程练习题5.45计算数字的标准差。本题使用一个和他不同但是等价的公式来计算n个数的标准差。
    在这里插入图片描述
    要用这个公式计算标准差,必须使用一个数组存储每个数,这样可以在获取平均值后使用他们。程序应该包含下面的方法:
    public static double devition(double[] x)
    public static double mean(double[] x)
    编写测试程序,提示用户输入10个数,然后显示平均值和标准差,如下面的运行示例所示:
    Enter 10 numbers: 1.9 2.5 3.7 2 1 6 3 4 5 2
    The mean is 3.1100000000000003
    The standard devition is 1.5573838462127583
    *7.11(Statistics: calculating standard deviation)Program exercise 5.45 calculate the standard deviation of the number. This problem uses a different but equivalent formula to calculate the standard deviation of N numbers.
    在这里插入图片描述
    To use this formula to calculate the standard deviation, you must use an array to store each number so that you can use them after you get the average. The program should include the following methods:
    public static double devition(double[] x)
    public static double mean(double[] x)
    Write a test program, prompt the user to enter 10 numbers, and then display the average value and standard deviation, as shown in the following running example:
    Enter 10 numbers: 1.9 2.5 3.7 2 1 6 3 4 5 2
    The mean is 3.1100000000000003
    The standard devition is 1.5573838462127583

  • 参考代码:

    package chapter07;
    
    import java.util.Scanner;
    
    public class Code_11 {
        public static void main(String[] args) {
            double[] numbers = new double[10];
            Scanner input = new Scanner(System.in);
            System.out.print("Enter 10 numbers: ");
            for (int i = 0;i < numbers.length;i++)
                numbers[i] = input.nextDouble();
            System.out.println("The mean is " + mean(numbers));
            System.out.println("The standard devition is " + devition(numbers));
        }
        public static double devition(double[] x){
            double sum = 0;
            for (int i = 0;i < x.length;i++)
                sum += Math.pow(x[i] - mean(x),2);
            return Math.sqrt(sum / (x.length - 1));
        }
        public static double mean(double[] x){
            double sum = 0;
            for (int i = 0;i < x.length;i++)
                sum += x[i];
            return sum / x.length;
        }
    }
    
    
  • 结果显示:

    Enter 10 numbers: 1.9 2.5 3.7 2 1 6 3 4 5 2
    The mean is 3.1100000000000003
    The standard devition is 1.5573838462127583
    
    Process finished with exit code 0
    
    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值