方法上的类型不兼容

时间:2012-05-31 06:55:30

标签: java incompatibletypeerror

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

我太累了,我在这个练习中做得很远,我甚至不确定它是否打印出正确的答案,但现在告诉我         double stdD = getD(avg,numbers); 有不兼容的类型,不确定什么是不兼容的 提前致谢

2 个答案:

答案 0 :(得分:6)

getD无效,它不返回值。目前正在

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

但它应该是

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

答案 1 :(得分: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)