JAVA

Sunday, November 20, 2005

Homework Lab 10-31 (2) Mean & Variance

import java.io.*;
public class dog
{ public static void main(String args[]) throws IOException
{ String str;
BufferedReader a;
int i=0;
float n,sum=0,sum1=0,j,q;
a=new BufferedReader(new InputStreamReader(System.in));
System.out.println("輸入一個數:");
str=a.readLine(); n=Integer.parseInt(str);
while(n>=0) {
sum=sum+n;
sum1=sum1+(n*n);
i++;
System.out.println("輸入一個數:");
str=a.readLine();
n=Integer.parseInt(str);
}
System.out.println("總值為:"+sum);
System.out.println("E(X)為:"+(float)(sum/i));
System.out.println("E(X*X)為:"+(float)(sum1/i));
System.out.println("又知道variance公式為:E(x*x)-(E(x)*E(x))");
j=(float)(sum/i);
q=(float)(sum1/i);
System.out.println("variance為:"+(q-(j*j)));
}
}

圖檔

0 Comments:

Post a Comment

<< Home