public class Test {
成都创新互联专注于屏边企业网站建设,响应式网站建设,购物商城网站建设。屏边网站建设公司,为屏边等地区提供建站服务。全流程按需搭建网站,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务
public static void main(String[] args) {
System.out.println("请输入10个double类型的数,并以空格隔开:");
Scanner input = new Scanner(System.in);
double[] arr = new double[10];
for (int i = 0; i 10; i++) {
arr[i] = input.nextDouble();
}
System.out.println(min(arr));
System.out.println(max(arr));
System.out.println("平均数:" + avg(arr));
System.out.println(standard(arr));
}
/**
* 标准差
*/
public static String standard(double[] arr) {
double sum = 0;
double avg = avg(arr);
for (int i = 0; i arr.length; i++) {
sum = sum + Math.pow((arr[i] - avg), 2);
}
return "标准差:" + Math.sqrt(sum/arr.length);
}
/**
* 平均值
*/
public static double avg(double[] arr) {
double sum = 0;
for (double a : arr) {
sum = sum + a;
}
return sum / arr.length;
}
/**
* 最大值
*/
public static String max(double[] arr) {
Arrays.sort(arr);
return "最大值:" + arr[arr.length-1];
}
/**
* 最小值
*/
public static String min(double[] arr) {
Arrays.sort(arr);
return "最小值:" + arr[0];
}
}
你好,先获取平均值,然后才可以获取标准差
public class GetAverageandStandardDevition {
private int[] array = new int[10];
private int num = 10;
public int getRandomDigit() {
return (int) (Math.random() * 1000);
}
public void getTargetDigit() {
for (int i = 0; i num; i++) {
array[i] = getRandomDigit();
System.out.println(array[i]);
}
}
//获取平均值
public double getAverage(){
int sum = 0;
for(int i = 0;i num;i++){
sum += array[i];
}
return (double)(sum / num);
}
//标准差
public double getStandardDevition(){
double sum = 0;
for(int i = 0;i num;i++){
sum += Math.sqrt(((double)array[i] -getAverage()) * (array[i] -getAverage()));
}
return (sum / (num - 1));
}
public static void main(String[] args) {
GetAverageandStandardDevition gcs = new GetAverageandStandardDevition();
gcs.getTargetDigit();
System.out.println(gcs.getAverage() + " " + gcs.getStandardDevition());
}
}
这个例子可以实现你的功能,数据源自己提供!
import java.util.Scanner;
public class NumTest {
public static void main(String[] args){
double num=0;
Scanner s = new Scanner(System.in);
System.out.println("请输入你需要计算的个数:");
int n = s.nextInt();
double[] list=new double[n];
for (int i = 1; i = n; i++) {
System.out.println("请输入第"+i+"个数:");
list[i-1]=s.nextDouble();
num+=list[i-1];
}
double mean=num/n;
double var=0;
for(double d:list){
var+=(d-mean)*(d-mean);
}
System.out.println("方差是:"+var/n);
}
}
参考如下
public static main(String[] args){
int[] is={3,2,5,35,17,28,21,45,7};
int max = is[0];
int min = is[0];
int sum = 0;
double ave = 0.0;
double StanDev=0.0;
for(int i=0;iis.length;i++){
if(is[i]max){
max=is[i];
}
if(is[i]min){
min=is[i];
}
sum+=is[i];
}
ave = sum*1.0/is.lenght;
for(int i = 0;i is.length;i++){
sum += Math.sqrt(((double)is[i] -ave) * (is[i] -ave));
}
StanDev=(sum / (num - 1));
System.out.println("最小是:"+min);
System.out.println("最大是:"+max);
System.out.println("平均值是:"+ave);
System.out.println("标准差是:"+ StanDev );}
Population standard deviation:
如是总体(即估算总体方差),根号内除以n MySql对应的函数是STD(对应excel函数:STDEVP);
Sample standard deviation:
如是抽样(即估算样本方差),根号内除以(n-1)MYSql 对应的函数是 STDDEV_SAMP (对应excel函数:STDEV);