问题:运用BMI及相关参考标准(Tab1.1),衡量人体胖瘦程度。
思路:
Step 1 :表格拆解(明确各分段函数)
Step 2:构造关系节点(Mapping)
package bmi;
/**
* @see 关系节点
* @author JustBeGeek
*/
public class Mapping{
private int gradation;
private String degree;
public Mapping(int gradation,String degree){
this.gradation=gradation;
this.degree=degree;
}
public int getGradation(){
return gradation;
}
public String getGrade(){
return degree;
}
}
Step 3 :存储各参考标准(构造分段函数)
/**
* @see 参考标准集
*/
/*世卫组织参考标准*/
public static final Mapping[] WHO_RS={
new Mapping(0,"偏瘦"), new Mapping(18.5,"正常"),
new Mapping(25,"偏胖"), new Mapping(30,"肥胖Ⅰ"),
new Mapping(35,"肥胖Ⅱ"), new Mapping(40,"肥胖Ⅲ")
};
/*亚洲参考标准*/
public static final Mapping[] Asia_RS={
new Mapping(0,"偏瘦"), new Mapping(18.5,"正常"),
new Mapping(23,"偏胖"), new Mapping(25,"肥胖Ⅰ"),
new Mapping(30,"肥胖Ⅱ"), new Mapping(40,"肥胖Ⅲ")
};
/*中国参考标准*/
public static final Mapping[] China_RS={
new Mapping(0,"偏瘦"), new Mapping(18.5,"正常"),
new Mapping(24,"偏胖"), new Mapping(28,"肥胖"),
new Mapping(40,"极度肥胖")
};
注:每个关系节点的分界值域存储 对应BMI范围的下界。
Step 4 :BMI计算
/**
* @see 计算BMI
* @param weight 体重(kg)
* @param height 身高(m)
* @return
*/
public static double compute_BMI(double weight,double height){
return (int)(weight/Math.pow(height, 2)*100)/100.0;
}
Step 5:编写BMI评价函数(函数求值)
/**
* @see BMI评价
* @param rs 参考标准
* @param bmi 身体质量指数
* @return
*/
public static String evaluate_BMI(Mapping[] rs,double bmi){
int i=rs.length-1;
for(;i>=0;i--){
if(bmi>=rs[i].getGradation())
break;
}
return rs[i].getDegree();
}
Step 6:集成并运行评价程序
package bmi;
import java.util.Scanner;
import bmi.ReferenceStandard;
/**
* @see BMI评价集成程序
* @author JustBeGeek
*/
public class Evaluate {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("请输入您的体重(kg):");
double weight=input.nextDouble();
System.out.println("请输入您的身高(m):");
double height=input.nextDouble();
double bmi=ReferenceStandard.compute_BMI(weight, height);
String degree=ReferenceStandard.evaluate_BMI(ReferenceStandard.Asia_RS, bmi);
System.out.println("您的身体"+degree);
}
}
相关类完整代码(实现):
package bmi;
import bmi.Mapping;
/**
* @see 参考标准
* @author JustBeGeek
*/
public class ReferenceStandard {
/**
* @see 参考标准集
*/
/*世卫组织参考标准*/
public static final Mapping[] WHO_RS={
new Mapping(0,"偏瘦"), new Mapping(18.5,"正常"),
new Mapping(25,"偏胖"), new Mapping(30,"肥胖Ⅰ"),
new Mapping(35,"肥胖Ⅱ"), new Mapping(40,"肥胖Ⅲ")
};
/*亚洲参考标准*/
public static final Mapping[] Asia_RS={
new Mapping(0,"偏瘦"), new Mapping(18.5,"正常"),
new Mapping(23,"偏胖"), new Mapping(25,"肥胖Ⅰ"),
new Mapping(30,"肥胖Ⅱ"), new Mapping(40,"肥胖Ⅲ")
};
/*中国参考标准*/
public static final Mapping[] China_RS={
new Mapping(0,"偏瘦"), new Mapping(18.5,"正常"),
new Mapping(24,"偏胖"), new Mapping(28,"肥胖"),
new Mapping(40,"极度肥胖")
};
/**
* @see 计算BMI
* @param weight 体重(kg)
* @param height 身高(m)
* @return
*/
public static double compute_BMI(double weight,double height){
return (int)(weight/Math.pow(height, 2)*100)/100.0;
}
/**
* @see BMI评价
* @param rs 参考标准
* @param bmi 身体质量指数
* @return
*/
public static String evaluate_BMI(Mapping[] rs,double bmi){
int i=rs.length-1;
for(;i>=0;i++){
if(bmi>=rs[i].getGradation())
break;
}
return rs[i].getDegree();
}
}
package bmi;
/**
* @see 关系节点
* @author JustBeGeek
*/
public class Mapping{
private double gradation;
private String degree;
public Mapping(double gradation,String degree){
this.gradation=gradation;
this.degree=degree;
}
public double getGradation(){
return gradation;
}
public String getDegree(){
return degree;
}
}
为什么这么做?可参考 案例分析:百分制转等级制程序演进