代码:(给出BMI的相关注释)
abstract class People{
int weight;
int height;
public People(int w, int h) {
this.weight = w;
this.height = h;
}
abstract public float stature();
public void drawResult() {
System.out.print("体重:"+this.weight+"(KG) 身高:"+this.height+"(cm)");
// BMI
// float statureResult = this.stature();
// if (statureResult<= 18.4) {
// System.out.println("体型:偏瘦");
// }else if (statureResult <= 23.9 && statureResult > 18.4) {
// System.out.println("体型:正常");
// }else if (statureResult < 23.9 && statureResult <= 27.9) {
// System.out.println("体型:超重");
// }else if (statureResult > 27.9) {
// System.out.println("体型:肥胖");
// } else {
// System.out.println("啥,应该不会运行到这里吧?");
// }
float statureResult = this.weight/this.stature()-1;
if(statureResult>=-0.1&&statureResult<=0.1) {
System.out.println("体型:标准");
}else if (statureResult<-0.1) {
System.out.println("体型:偏瘦");
}else if (statureResult>0.1) {
System.out.println("体型:偏重");
}else {
System.out.println("啥,应该不会运行到这里吧?嘿嘿嘿");
}
};
}
class Woman extends People {
Woman(int w, int h){
super(w, h);
}
@Override
public float stature() {
//BMI=体重(千克)除以身高(米)的平方(kg/m2)
//return this.weight/((this.height/100)^2);
// 女性:(身高cm-70)×60﹪=标准体重
return (float) ((this.height-70.0)*0.6);
}
}
class man extends People {
man(int w, int h){
super(w, h);
}
@Override
public float stature() {
//BMI=体重(千克)除以身高(米)的平方(kg/m2)
//return this.weight/((this.height/100)^2);
//男性:(身高cm-80)×70﹪=标准体重
return (float) ((this.height-80.0)*0.7);
}
}
public class PeopleTest {
public static void main(String[] args) {
System.out.print("这个男士:");
man Man = new man(100, 185);
Man.drawResult();
}
}
运行效果: