BMI计算器问题

时间:2015-10-31 06:29:31

标签: java math constructor calculator

在没有让BMR计算器按预期工作之后,我决定采用更简单的方法,并尝试为BMI制作一个计算器(因为它不需要不同的性别方法)或多或少会从BMR计算器重写。问题是我对我应该如何使用数学不太自信,因为我认为我可以完全回答并让它作为浮点数返回,但是当我使用测试时它告诉我,人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;
  }
}
&#13;
&#13;
&#13;

BMI测试类

&#13;
&#13;
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());

  }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

您要分配参数,而不是字段。此

getActivity()

应该是

getApplicationContext()