简体   繁体   English

方法不兼容的类型

[英]Incompatible types on method

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

Im so tired and I got this far into this exercise, I'm not even sure if it prints out the correct answer but right now its telling me double stdD = getD(avg,numbers); 我太累了,我已经很累了,我什至不确定它是否能打印出正确的答案,但现在它告诉我double stdD = getD(avg,numbers); that there are incompatible types, not sure what is incompatible Thanks in advance 有不兼容的类型,不确定什么不兼容预先感谢

getD is void, it doesn't return a value. getD是无效的,它不返回值。 It's currently 目前是

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

but it should probably be 但这应该是

public static double getD(double avg, ArrayList<Double> numbers){
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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM