2012-05-31 67 views
0
import java.util.*; 
import java.lang.Math; 

class StandardDeviation{ 
    public static void main(String args[]){ 
    ArrayList<Double> numbers = new ArrayList<Double>(); 
    Scanner kb=new Scanner(System.in); 
    double in=kb.nextDouble(); 
    while(in!=-1){ 
     numbers.add(kb.nextDouble()); 
    } 
    double avg = getAvg(numbers); 
    double stdD = getD(avg,numbers);// tells me these are incompatible types 
    System.out.printf("%f\n",stdD); 
    } 

    public static double getAvg(ArrayList<Double> numbers){ 
    double sum = 0.0; 
    for (Double num : numbers){ 
     sum+= num; 
    } 
    return sum/numbers.size(); 
    } 

    public static void getD(double avg, ArrayList<Double> numbers){ 
    ArrayList<Double> newSet = new ArrayList<Double>(); 
    for (int i = 0; i < numbers.size(); i++){ 
     newSet.add(Math.pow(numbers.get(i)-avg,2)); 
    } 
    double total = 0.0; 
    for (Double num : newSet){ 
     total += num; 
    } 
    double mean =(total/numbers.size()); 
    return Math.sqrt(mean); 
    } 
} 

我太疲倦,我能走到今天這個運動,我甚至不知道這是否是打印出正確的答案,但現在它告訴我 雙STDD = GETD(AVG,數字); 有不兼容的類型,不知道什麼是不兼容的 預先感謝類型不兼容的方法

回答

6

getD是無效的,它不會返回一個值。這是目前

public static void getD(double avg, ArrayList<Double> numbers){ 

但它可能應該是

public static double getD(double avg, ArrayList<Double> numbers){ 
+0

呵呵。我認爲從void中返回一個值是錯誤的。 – Tharwen

+0

但它是一個錯誤?他告訴我如何解決它.. –

+0

@Tharwen好吧,有兩個錯誤。第一,他試圖將一個void寫入一個變量,其次嘗試以void方法返回一個值。但是因爲他想在double變量中寫一個值,所以實際的錯誤是void返回類型,而不是void方法中的返回值。 – tagtraeumer

3
Your method is not returning anything. method return type is void and you are assigning that into double. that's why you are getting an error. Your method declaration should be like this - 

public static double getD(double avg, ArrayList<Double> numbers)