Java求方差和標(biāo)準(zhǔn)差:
依安網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),依安網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為依安1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的依安做網(wǎng)站的公司定做!
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);
}
//標(biāo)準(zhǔn)差
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("請(qǐng)輸入你需要計(jì)算的個(gè)數(shù):");
int n = s.nextInt();
double[] list=new double[n];
for (int i = 1; i = n; i++) {
System.out.println("請(qǐng)輸入第"+i+"個(gè)數(shù):");
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);
}
}
現(xiàn)在學(xué)習(xí)Java的平臺(tái)有很多,B站或者是騰訊課堂都是不錯(cuò)的,我們?cè)贐站分享了很多經(jīng)典的Java視頻教程,都是開源的,你可以去看看。