BMI计算器Java

时间:2017-05-30 03:49:17

标签: java

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

nullweighs 0.0,测量值为0.0。您的BMI

何时应说明姓名,体重,BMI以及体重不足,健康,超重或肥胖。

以下是代码:

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;


    }

和....

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

    }

3 个答案:

答案 0 :(得分:4)

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

由于您自动使用0.0和0.0的值,您的BMI计算也会关闭。您应该使用Person类中的getter方法进行计算。

答案 1 :(得分:1)

您是否应该如下计算BMI?:

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,而不必请求权重和身高作为参数:

public double calculateBMI() {
   return ((this.weightPounds / this.heightInches) * (this.heightInches)) * 703;

}

检查零输入也是一件好事。

答案 2 :(得分:0)

嗨,你需要一些基本的OOP练习。我修复了你发布的代码。

  • 已修复constructor injection(您将变量设置为与其无关的变量。)
  • ToString()方法不需要使用this访问其字段。(已删除此内容)
  • 计算BMI与人及其领域本身有关。因此不需要参数化。(删除calculateBMI参数)
  • 将代码重新格式化为Google Code Style

以下是您的课程:

<强>人

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 = userHeightInches;
   this.weightPounds = userWeightPounds;
}

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 firstName + "weighs " + weightPounds + " and measures  "
    + heightInches + " tall. Your BMI is ";
}

public double calculateBMI()
{
  double BMI = ((weightPounds / heightInches) * (heightInches)) * 703;
  return BMI;
}
}

<强> BodyMassIndex

import java.util.Scanner;

public class BodyMassIndex {

    public static Person anyPerson;

    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 BMI = anyPerson.calculateBMI();

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

祝你好运!