在没有让BMR计算器按预期工作之后,我决定用一些简单一点的方法来做BMI的计算器(因为它不需要性别的不同方法),它或多或少会被BMR计算器重写。问题是,我对我应该如何使用这个数学不太自信,因为我认为我可以舍弃答案,让它作为浮动返回,但是当我使用测试类时,它告诉我,person 1的BMI是0。在将calc方法保持为浮点数的同时,有什么方法可以修复这个问题吗?
BMI类
public class BMI {
private String name;
private float weight; //Meters
private float height; //Kilograms
public BMI(String n, float w, float h) //CONSTRUCTOR
{
n = name;
w = weight;
h = height;
}
public float calculateBMI() //MATH CALCULATIONS
{
return Math.round((weight * 2.20462) / (height * 39.3701)); //The numbers mulitplied by height & weight are the conversions
}
public String catagoryBMI() {
String getcata;
if (calculateBMI() <= 15) {
getcata = "Very severely underweight";
} else if (calculateBMI() < 16.0) {
getcata = "Severely underweight";
} else if (calculateBMI() < 18.0) {
getcata = "Underweight";
} else if (calculateBMI() < 25) {
getcata = "Normal (healthy weight)";
} else if (calculateBMI() < 30) {
getcata = "Overweight";
} else if (calculateBMI() < 35) {
getcata = "Obese Class I (Moderately obese)";
} else if (calculateBMI() < 40) {
getcata = "Obese Class II (Severely obese)";
} else {
getcata = "Obese Class III (Very severely obese)";
}
return getcata;
}
}
体质指数测试类别
public class BMITest {
public static void main(String[] args) {
BMI bmi1 = new BMI("BMI TEST1", 65.7709f, 1.79832f);
//p1BMI = bmi1.calculateBMI();
//p1CATA = bmi1.catagoryBMI();
System.out.println("BMI PERSON 1: " + bmi1.calculateBMI());
System.out.println("CATAGORY PERSON 1: " + bmi1.catagoryBMI());
}
}
发布于 2015-10-31 06:31:10
你是在分配给参数,而不是字段。这
public BMI(String n, float w, float h) //CONSTRUCTOR
{
n = name;
w = weight;
h = height;
}应该是这样的
public BMI(String n, float w, float h) //CONSTRUCTOR
{
name = n;
weight = w;
height = h;
}https://stackoverflow.com/questions/33448767
复制相似问题