2016-10-09 2 views
-2

표준 편차를 찾으려면 코드가 잘못되었습니다. 내 코드는 사용자 입력으로부터 표준 편차를 찾는다. 나는 숫자 1 2 3을 입력했고이 숫자 세트의 표준 편차는 1이지만 잘못 입력 한 곳은 10을 인쇄했습니다. 또한 나는 그들이 사용하지 않는 변수들을 많이 가지고 있다는 것을 알고있다.표준 편차 계산에 어떤 문제가 있습니까?

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

하나의 변수에 대한 표준 편차는 here으로 정의됩니다. 관측치와 세트 평균의 제곱 차이의 합 평균의 제곱근을 취해야합니다.

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

이렇게하면 필요한 것을 얻을 수 있습니다.