定义一个People类,要求如下:
(1)成员变量:name、height、weight分别表示姓名、身高cm和体重kg
(2)构造方法通过参数实现对成员变量的赋初值操作。
(3)成员方法int check(),方法返回0,1,-1分别表示标准、过胖或过瘦。
判断方法:用身高减去110作为参考体重,超过参考体重5kg以上的,过胖;低于参考体重5kg以上的,过瘦;在参考体重上下5kg以内的为标准。
(4)main方法中,输入50个人的信息(姓名,身高,体重),分别输出标准,过胖,过瘦的人数,必须通过调用check方法实现
这个题目的说用check方法返回 0 1 -1 但我怎么在main里去统计个数呢
对这里学习的很模糊 求一份代码 学习一下
package cn.base;
/**
* @Description:
* @Author :小书包
* @CreateDate :2018-11-23 19:15
*/
public class People {
// 姓名
private String name;
// 身高
private double height;
// 体重
private double weight;
// 构造方法
public People(String name, double height, double weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
// 检查体重
public int checkWeight() {
// 获得标准体重
double standardWeight = this.height - 110;
if (weight >= (standardWeight + 5)) {
return 1;
}
if (weight <= (standardWeight - 5)) {
return -1;
}
return 0;
}
public static void main(String[] args) {
int[] arr = new int[50];
for (int i = 0; i < 50; i++) {
int result = new People("张三"+i,100,50).checkWeight();
arr[i] = result;
}
int heavyNums = 0;
int thinNums = 0;
int normalNums = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 1) heavyNums++;
if (arr[i] == -1) thinNums++;
if (arr[i] == 0) normalNums++;
}
System.out.println("正常人数:" + normalNums);
System.out.println("瘦的人数:" + heavyNums);
System.out.println("胖的人数:" + thinNums);
}
}