gpt4 book ai didi

java - BMI 计算器问题

转载 作者:行者123 更新时间:2023-11-30 08:07:52 26 4
gpt4 key购买 nike

在没有让 BMR 计算器按预期工作后,我决定使用一些更简单的东西,并尝试制作一个 BMI 计算器(因为它不需要不同的性别方法),或多或少从 BMR 计算器重写。问题是我对我应该如何使用数学并没有真正的信心,因为我认为我可以四舍五入答案并让它作为 float 返回但是当我使用测试类时它告诉我人 1 的 BMI 为 0。有什么方法可以解决此问题,同时将 calc 方法保持为 float ?

体重指数等级

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;
}
}

BMI测试类

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());

}
}

最佳答案

您分配给参数,而不是字段。这个

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;
}

关于java - BMI 计算器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33448767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com