Java中计算学生的平均身高

Students类中定义身高属性
Height类中定义方法,传递学生对象数组,求平均身高
测试类调用Height类的方法

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

纯手打,测试可用!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-19
楼主,计算平均,就是身高加起来,除以人数。本回答被网友采纳
第2个回答  2017-11-09
student{
public int high;//学生身高
}
Height{
public void veg(stu []){
int sum=0;
int avg=0;//平均身高

for(int i=0;i<stu.length;i++){
num+=stu[i].high;

}
avg=num/str.length;

System.out.println("学生身高为: "+avg);

}

}
相似回答