第五章第四十五题(统计:计算平均值和标准方差)(Statistics: compute mean and standard deviation)

第五章第四十五题(统计:计算平均值和标准方差)(Statistics: compute mean and standard deviation)

  • **5.45(统计:计算平均值和标准方差)在商务应用程序中经常需要计算数据的平均值和标准方差。平均值就是数字的简单平均。标准方差则是一个统计数字,给出了在一个数字集中各种数据距离平均值的聚集紧密度。例如,一个班级的学生的平均年龄是多少?年龄相差近吗?如果所有的学生都是同龄的,那么方差为0。编写一个程序,提示用户输入10个数字,然后运用下面的公式,显示这些数字的平均值及标准方差。在这里插入图片描述
    下面是一个运行示例:
    Enter 10 numbers: 1 2 3 4.5 5.6 6 7 8 9 10
    The mean is 5.61
    The standard deviation is 2.99794
    **5.45(Statistics: compute mean and standard deviation)In business applications, you are often asked to compute the mean and standard deviation of data. The mean is simply the average of the numbers. The standard deviation is a statistic that tells you how tightly all the various data are clustered around the mean in a set of data. For example, what is the average age of the students in a class? How close are the ages? If all the students are the same age, the deviation is 0. Write a program that prompts the user to enter 10 numbers and displays the mean and standard deviations of these numbers using the following formula:
    在这里插入图片描述
    Here is a sample run:
    Enter 10 numbers: 1 2 3 4.5 5.6 6 7 8 9 10
    The mean is 5.61
    The standard deviation is 2.99794
  • 参考代码:
package chapter05;

import java.util.Scanner;

public class Code_45 {
    public static void main(String[] args) {
        double sumOfSquares = 0, sum = 0, tempNumber;
        Scanner inputScanner = new Scanner(System.in);
        System.out.print("Enter 10 numbers: ");
        for(int i = 1;i <= 10;i++) {
            tempNumber = inputScanner.nextDouble();
            sumOfSquares += tempNumber * tempNumber;
            sum += tempNumber;
        }
        System.out.printf("The mean is %.2f\n", sum / 10);
        System.out.printf("The standard deviation is %.5f", Math.sqrt((sumOfSquares - (sum * sum / 10)) / 9));

        inputScanner.close();
    }

}

  • 结果显示:
Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10
The mean is 5.50
The standard deviation is 3.02765
Process finished with exit code 0

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值