2012-05-31 6 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 (평균, 숫자를 말하는); 호환되지 않는 유형의

답변

6

getD 무효 무엇인지 사전에 호환되지 않는 감사입니다 확실하지 호환되지 않는 유형이 있다는 것을,이 값을 반환하지 않습니다. 그것은 현재

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

입니다하지만 아마

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

허해야한다. 나는 공백에서 가치를 되 찾는 것이 잘못이라고 생각했습니다. – Tharwen

+0

하지만 오류입니까? 그는 그것을 해결하는 방법을 말해 줬어 .. –

+0

@ Tarwen 물론 괜찮아요 두 가지 오류가 발생했습니다. 첫 번째는 변수에 void를 쓰려고 시도한 후 두 번째로 void 메서드에서 값을 반환하려고 시도한 것입니다. 하지만 double 변수에 값을 쓰고 싶었 기 때문에 실제 오류는 void 메서드의 반환이 아닌 void 반환 유형이었습니다. 나는 당신의 대답을 이해 @tagtraeumer – 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) 
관련 문제