public class Computer{
public double getResult(double a,int[] x){
double result=0;
int sum=0;
for(int i=0;i<x.length;i++){
sum=sum+x[i];
}
result=a*sum;
return result;
}
}
public class Example4_6{
public static void main(String args[]){
Computer computer=new Computer();
double result=computer.getResult(1.0/3,10,20,30);
System.out.println("10,20,30的平均数:"+result);
result=computer.getResult(1.0/6,66,12,5,89,2,51);
System.out.println("66,12,5,89,2,51的平均数:"+result);
}
}
------解决方案--------------------------------------------------------
你调用的getResult的形参是一个int数组,你使用的不对
- Java code
class Computer{ public double getResult(double a,int[] x){ double result=0; int sum=0; for(int i=0;i<x.length;i++){ sum=sum+x[i]; } result=a*sum; return result; }}public class Example4_6{ public static void main(String args[]){ Computer computer=new Computer(); double result=computer.getResult(1.0/3,new int[]{10,20,30}); System.out.println("10,20,30的平均数:"+result); result=computer.getResult(1.0/6,new int[]{66,12,5,89,2,51}); System.out.println("66,12,5,89,2,51的平均数:"+result); }}