java编程,第二题去掉最大最最小值后的平均值
1个回答
展开全部
按照你的要求编写的Java程序如下
ComputerAverage.java
public interface ComputerAverage {
public double average(double x[]);
}
Gymnastics.java
public class Gymnastics implements ComputerAverage{
@Override
public double average(double[] x) {
double sum=0;
double max=x[0];
double min=x[0];
if(x.length-2<=0){
System.out.println("输入的数组长度小于等于2!");
return 0;
}
for(int i=1;i<x.length;i++){
if(max<x[i]) max=x[i];
if(min>x[i]) min=x[i];
}
for(int i=0;i<x.length;i++){
if(max==x[i] || min==x[i]){
}else{
sum=sum+x[i];
}
}
return sum/(x.length-2);
}
}
School.java
public class School implements ComputerAverage{
@Override
public double average(double[] x) {
double sum=0;
for(int i=0;i<x.length;i++){
sum=sum+x[i];
}
return sum/x.length;
}
}
Estimator.java
public class Estimator {
public static void main(String[] args) {
double a[]={9.89,9.88,9.99,9.12,9.69,9.76,8.97};
double b[]={89,56,78,90,100,77,56,45,36,79,98};
ComputerAverage computer;
computer=new Gymnastics();
double result=computer.average(a);
System.out.println("体操选手最后得分:"+result);
computer=new School();
result=computer.average(b);
System.out.println("班级考试平均分数:"+result);
}
}
注意 以上四个类要求在同一个包下
运行结果
体操选手最后得分:9.668
班级考试平均分数:73.0909090909091
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询