package yourPackageName;
import java.math.BigDecimal;
public class People {
// (1)成员变量:name、height、weight分别表示姓名、身高(cm)和体重(kg)。
// 姓名
private String name;
// 身高(cm)
private int height;
// 体重(kg)
private int weight;
/**
* 构造方法通过参数实现对成员变量的赋初值操作
*/
People(String name, int height, int weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
/**
* 该方法返回0、1、-1分别表示标准、过胖或过瘦)。 判断方法是:用身高减去110作为参考体重,超过参考体重5kg以上的,为“过胖”;
* 低于参考体重5kg以上的 ,为“过瘦”;在(参考体重-5kg)和(参考体重+5kg)之间的,为“标准”。
*/
int check() {
int standard = this.height - 110;
if (this.weight > standard + 5) {
return 1;
}
if (this.weight < standard - 5) {
return -1;
}
return 0;
}
@Override
public String toString() {
return new StringBuffer(this.name).append(",").append(this.height).append("cm,").append(this.weight).append("kg,").append(this.check()).toString();
}
/**
* (4)在main方法中,输入50个学生的信息(姓名、身高和体重),分别输出标准、过胖或过瘦的人数(必须通过调用check()方法实现)。
*
* @return void
*/
public static void main(String[] args) {
int aCount = 0;
int bCount = 0;
int cCount = 0;
for (int i = 0; i < 50; i++) {
// 创建50个身材体重随机的People
People man = new People("People" + i, new BigDecimal(Math.random() * 50).intValue() + 150, new BigDecimal(Math.random() * 60).intValue() + 40);
switch (man.check()) {
case 0:
aCount++;
break;
case 1:
bCount++;
break;
case -1:
cCount++;
break;
}
}
System.out.println("标准身材:" + aCount + "个");
System.out.println("过胖:" + bCount + "个");
System.out.println("过瘦:" + cCount + "个");
}
}
温馨提示:内容为网友见解,仅供参考