1:第1个类:package com.example;
public class Three1 {
//定义身高属性:
public float height=0f;
}
2:第2个类:package com.example;
public class Three2 {
public float height(Three1 [] stu){
float avgHeight=0f;
float all=0f;
int count=0;
for (int i = 0; i < stu.length; i++) {
if (stu[i].height !=0) {
all=all+stu[i].height;
count++;
}
}
avgHeight=all/count;
return avgHeight;
}
}
第3个测试类:import java.util.Scanner;
import com.example.Three1;
import com.example.Three2;
public class ThreeTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义包含5个元素的学生对象数组:
Three1 [] arr1=new Three1[5];
Three2 duixiang=new Three2();
Scanner input=new Scanner(System.in);
for (int i = 0; i < arr1.length; i++) {
System.out.println("请输入第"+(i+1)+"名学生的身高:");
arr1[i]=new Three1();//实例化
arr1[i].height=input.nextFloat();
}
//调用方法:
float avg=duixiang.height(arr1);
System.out.println(avg);
}
}
请教各位大侠:第2个类中:stu[i].height 是什么意思,怎么解释?2:测试类中:arr1[i]=new Three1(); 是什么意思,怎么解释?求详细的解答理论!