首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BMI计算器Java

BMI计算器Java
EN

Stack Overflow用户
提问于 2017-05-30 03:49:16
回答 2查看 2.3K关注 0票数 0

我正在创建一个BMI计算器,但是我在运行程序时遇到了问题,因为我的结果是:

值为0.0,身高为0.0。你的体重指数是

当它应该说明名字,体重,体重指数,以及它是体重不足,健康,超重,还是肥胖。

以下是代码:

代码语言:javascript
复制
public class Person {

    private String firstName;
    private double  heightInches;
    private double weightPounds;


    public Person (String userFirstName, double userHeightInches, double userWeightPounds)
    {


        this.firstName = firstName;
        this.heightInches = heightInches;
        this.weightPounds = weightPounds;


    }



    public String getFirstName()
    {
        return this.firstName;
    }

    public double getHeightInches()
    {
        return this.heightInches;
    }

    public double getWeightPounds()
    {
        return this.weightPounds;
    }

    public void setFirstName (String firstName)
    {
        this.firstName = firstName;
    }

    public void setHeightInches(double userHeightInches)
    {
        this.heightInches = userHeightInches;
    }

    public void setWeightPounds(double userWeightPounds)
    {
        this.weightPounds = userWeightPounds;
    }





    @Override
    public String toString()
    {
        return this.firstName + "weighs " + this.weightPounds + " and measures  "
               + this.heightInches + " tall. Your BMI is "; 
    }



     public double calculateBMI(double userHeightInches,  double userWeightPounds )

    {


    double BMI = ((userWeightPounds / userHeightInches) * (userHeightInches)) * 703;
    return BMI;


    }

还有..。

代码语言:javascript
复制
public class BodyMassIndex {
    public static Person anyPerson;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 

    {

          createPersonObject();
          displayBMI();

    }

    public static void createPersonObject()
    {

        Scanner keyboard = new Scanner(System.in);


        System.out.println("What is your first name?");
        String firstName = keyboard.next();


        System.out.println ("How tall are you (in inches)?");
        Double heightInches = keyboard.nextDouble();



        System.out.println("How much do you weigh (in pounds)?");
        Double weightPounds = keyboard.nextDouble();

        anyPerson = new Person(firstName, heightInches, weightPounds);
    }

    public static void displayBMI()
    {


        double userWeightPounds = 0;
        double userHeightInches = 0;


        double BMI = anyPerson.calculateBMI(userWeightPounds, userHeightInches);

        System.out.println(anyPerson.toString());

        if (BMI < 18.5)
        {
            System.out.println("Underweight");
        }

        else if (BMI >= 18.5 && BMI < 24.9) {
            System.out.println("Healthy");
        }

        else if (BMI >= 25 && BMI < 29.9) {
            System.out.println("Overweight");
        }

        else if (BMI >= 30) {
            System.out.println("Obese");
        }

    }
EN

回答 2

Stack Overflow用户

发布于 2017-05-30 03:54:40

您正在将字段变量设置为自身,这些变量没有实例化,因此值为null或0.0。您需要将它们设置为类的参数: this.firstName =this.firstName

您的BMI计算也是关闭的,因为您自动使用的值为0.0和0.0。您应该使用Person类中的getter方法来进行计算。

票数 4
EN

Stack Overflow用户

发布于 2017-05-30 03:55:53

你应该按以下方式计算BMI吗?

代码语言:javascript
复制
public static void displayBMI() {

  /* This code is no longer needed */
  double userWeightPounds = 0;
  double userHeightInches = 0;

  double BMI = anyPerson.calculateBMI(anyPerson.getWeightPounds(), anyPerson.getHeightInches());
  ...

您还可以进一步简化代码,使Person不必要求将体重和身高作为参数来计算BMI:

代码语言:javascript
复制
public double calculateBMI() {
   return ((this.weightPounds / this.heightInches) * (this.heightInches)) * 703;

}

最好也检查一下零输入。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44253099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档