当前位置: 代码迷 >> Java相关 >> 有参方法 对类的调用
  详细解决方案

有参方法 对类的调用

热度:114   发布时间:2012-10-28 21:50:24.0
有参方法 对类的调用
public class Students {
    // 定义身高
   
    float height;
}

public class Height {
public float getAvgHeight(Students[] stu){
    float avgHeight=0;
    float all=0;//总身高
    int count=0;//学生计数
    for(int i=0;i<stu.length;i++){
        if(stu[i].height!=0){
            all+=stu[i].height;
            count++;
        }
    }
    avgHeight=all/count;
    return avgHeight;
}
}



public static void main(String[] args) {
        Students[] stu = new Students[5];
        Height h = new Height();
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            System.out.print("请输入第" + (i + 1) + "名学生的身高(cm):");
            stu[i] = new Students();// 实列化
            stu[i].height = input.nextFloat();

        }
        // 调方法
        float avgheight = h.getAvgHeight(stu);
        System.out.println("***这五名学生的平均身高为:" + avgheight + "");

    }

}


师哥师姐们红色代码有啥用  不要则下面不弄运行并提示没赋值   对类的调用搞不懂呀  对类的调用过程是啥????帮帮忙
搜索更多相关的解决方案: 身高  count  return  public  void  

----------------解决方案--------------------------------------------------------
去看看java句柄那是怎么回事  你就明白了
推荐书籍 thinking in java
----------------解决方案--------------------------------------------------------
Students[] stu = new Students[5];//实例化对象Students 长度为5的数组

stu[i] = new Students();//stu里是空的所以创建一个新的对象给他

stu[i].height //有了对象就是使用对象里的属性了
----------------解决方案--------------------------------------------------------
为对象分配内存空间
----------------解决方案--------------------------------------------------------
  相关解决方案