public class Compute {
class Student {// 学生类
private double height;
Student(double height) {
this.height = height;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
class Height {// 方法类
public double compute(Student[] students) {
int count = students.length;
double sum = 0;
for (Student student : students) {
sum += student.getHeight();
}
double result = sum / count;
return result;
}
}
class TestHeihgt {// 测试类
public void test() {
Student[] students = { new Student(170), new Student(160), new Student(165) };
Height height = new Height();
double avgHeight = height.compute(students);
System.out.println("学生平均身高是:" + avgHeight);
}
}
public static void main(String[] args) {
TestHeihgt testHeihgt = new Compute().new TestHeihgt();
testHeihgt.test();
}
}
纯手打,测试可用!
温馨提示:答案为网友推荐,仅供参考