2016-10-09 13 views
-2

標準偏差を見つけるためのコードが間違っています。私のコードは、ユーザー入力からの標準偏差を見つけるはずです。私は数字1 2 3をタイプし、この数字セットの標準偏差は1ですが、どこが間違っていたのですか?また、私は未使用の変数の束がそれらを気にしないことを知っています。標準偏差の計算に何が問題なのですか?

import java.util.Scanner; 

public class readFromKeyboard { 

    public static void main(String[] args) { 


     Scanner input = new Scanner(System.in); 

     String inStr = input.next(); 
     int n; 
     int i; 
     int count=0; 
     int min = Integer.MAX_VALUE; 
     int max = Integer.MIN_VALUE; 
     double average=0; 
     int sum; 
     double deviation = 0; 
     int total; 
     int temp = 0; 



     while (!inStr.equals("EOL")) { 
      count++; 
      n = Integer.parseInt(inStr); 
      min = Math.min(min, n); 
      max = Math.max(max, n); 
      System.out.printf("%d ", n); 
      inStr = input.next(); 
      average += n; 
      temp += Math.pow(n - average, 2); 

     } 

     deviation = temp 
     average = average/count; 

     System.out.println("\n The average of these numbers is " + average); 
     System.out.printf("The list has %d numbers\n", count); 
     System.out.printf("The minimum of the list is %d\n", min); 
     System.out.printf("The maximum of the list is %d\n", max); 
     System.out.printf("The standard deviation of the list is %d\n", temp); 

     input.close(); 


    } 
} 
+0

ループ内では、「平均」は実際には平均ではなく、累積合計です。私はあなたが実際の平均を持っている後に偏差を計算する必要があると思う –

+0

あなたのコードはあなたがそれを書いたものを正確にやっているが、標準偏差を計算するあなたの方法はかなり間違っています。このページを読んでコードを書き直してください(かなり)[標準偏差の計算](https://www.strchr.com/standard_deviation_in_one_pass) –

+0

私のコードは何ですか?私はそれが最初の入力番号を取って平均を減算してそれらを二乗すると思った –

答えて

0
import java.util.ArrayList; 
import java.util.Scanner; 

public class ReadFromKeyboard { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 

     String inStr = input.next(); 
     int n = 0; 
     int i; 
     int count = 0; 
     int min = Integer.MAX_VALUE; 
     int max = Integer.MIN_VALUE; 
     double average = 0; 
     int sum; 
     double deviation = 0; 
     int total; 
     double temp = 0;// correction here because it must store double or float value 

     ArrayList<Integer> n1 = new ArrayList<Integer>();// i used this to store all entered values 

     while (!inStr.equals("EOL")) { 
      count++; 
      n = Integer.parseInt(inStr); 
      min = Math.min(min, n); 
      max = Math.max(max, n); 
      System.out.printf("%d ", n); 
      n1.add(n); 
      inStr = input.next(); 
      average += n; 

     } 

     average = average/count; // this will give you final average 

     for (int j = 0; j < count; j++) { 
      temp += Math.pow(n1.get(j) - average, 2); 
     } 
     //System.out.println("\n" + temp + " " + count); 
     deviation = Math.sqrt(temp/count); // this is your standard deviation 

     //System.out.println(deviation); 

     System.out.println("\n The average of these numbers is " + average); 
     System.out.printf("The list has %d numbers\n", count); 
     System.out.printf("The minimum of the list is %d\n", min); 
     System.out.printf("The maximum of the list is %d\n", max); 
     System.out.println("The standard deviation of the list is " + deviation); 

     input.close(); 

    } 
} 
+0

@markusあなたのセットの標準偏差1 2 3は0.8165 –

+0

になりますそれは私にarraylistのエラーを与えます –

+0

あなたは "java .util.ArrayList "?? –

0

1つの変数の標準偏差がhereと定義されています。観測値と平均値の差の平方和の平方根を取る必要があります。

//in the loop 
temp += Math.pow(n - average, 2) 
//outside the loop 
deviation = Math.pow(temp/count,0.5) //or alternatively Math.sqrt() 

これは、必要なものを提供します。