2016-10-08 123 views
0

idk如果我必須在循環中或循環外獲得標準,也可以幫助我理解爲什麼它是在循環內或循環外進行,以及它們有什麼區別。我也知道在這種情況下,標準偏差公式就像(輸入 - 平均值)^ 2,然後再加上所有的值併除以平方根即可。我只是不完全知道如何寫,並把它放在哪裏如何獲得標準差

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 count=0; 
     int min = Integer.MAX_VALUE; 
     int max = Integer.MIN_VALUE; 
     double average=0; 
     int sum; 
     double deviation = 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; 
     } 

     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); 


     input.close(); 


    } 
} 
+0

您可以添加第二個循環來計算根據你的平均標準偏差。 – drum

+0

我應該寫一個循環,將每個數字除以平均值,然後在數字輸入時對其進行平方,然後將它們按數字相加,然後平方根呢? –

+0

我沒跟着。首先讓我們確保我們談論的是「標準偏差」,而不是「總體標準偏差」或「方差」。 – drum

回答

1

既然你已經計算average,現在你可以計算standard deviation每個號碼。

  1. 創建陣列sd[]來存儲standard deviation
  2. 對於每個編號,sd[i] = (average - input_i)^2

計算variance

  1. 對於每個standard deviationsd[],通過輸入總數添加到可變temp
  2. 鴻溝temp

計算population standard deviation

  1. 平方根variance