JAVA输入一组数,将其排序并求总体标准差,平均数,中位数,总体方差,样本方差,样本标准差,最后输出
时间: 2023-08-06 16:05:43 浏览: 139
以下是一个JAVA程序,可以实现输入一组数,将其排序并求总体标准差,平均数,中位数,总体方差,样本方差,样本标准差,并输出结果:
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一组数,以空格分隔:");
String[] strArr = input.nextLine().split(" ");
double[] nums = new double[strArr.length];
for (int i = 0; i < strArr.length; i++) {
nums[i] = Double.parseDouble(strArr[i]);
}
Arrays.sort(nums); // 排序
double sum = 0;
for (double num : nums) {
sum += num;
}
double avg = sum / nums.length; // 平均数
double stdDev = 0; // 总体标准差
double variance = 0; // 总体方差
double sampleVariance = 0; // 样本方差
double sampleStdDev = 0; // 样本标准差
for (double num : nums) {
stdDev += Math.pow(num - avg, 2);
}
stdDev = Math.sqrt(stdDev / nums.length);
for (double num : nums) {
variance += Math.pow(num - avg, 2);
}
variance = variance / nums.length;
for (double num : nums) {
sampleVariance += Math.pow(num - avg, 2);
}
sampleVariance = sampleVariance / (nums.length - 1);
sampleStdDev = Math.sqrt(sampleVariance);
double median;
if (nums.length % 2 == 0) {
median = (nums[nums.length / 2 - 1] + nums[nums.length / 2]) / 2; // 中位数
} else {
median = nums[nums.length / 2];
}
System.out.println("排序后的数组为:" + Arrays.toString(nums));
System.out.println("平均数为:" + avg);
System.out.println("总体标准差为:" + stdDev);
System.out.println("中位数为:" + median);
System.out.println("总体方差为:" + variance);
System.out.println("样本方差为:" + sampleVariance);
System.out.println("样本标准差为:" + sampleStdDev);
}
}
```
程序运行示例:
```
请输入一组数,以空格分隔:2.3 4.5 1.2 5.6 3.4
排序后的数组为:[1.2, 2.3, 3.4, 4.5, 5.6]
平均数为:3.4
总体标准差为:1.615549442140351
中位数为:3.4
总体方差为:2.6000000000000005
样本方差为:3.250000000000001
样本标准差为:1.8027756377319946
```
阅读全文