0

这就是我必须做的“编写一个 java 应用程序,提示输入该人的信息,为该人实例化一个 Health Profile 类的对象并打印来自该对象的信息——包括该人的名字、姓氏、性别 , 出生 日期 , 身高 和 体重 —— 然后 计算 并 打印 人 的 年龄 , BMI , 最大 心率 和 目标 心率 范围 . 它 还 应该 显示 练习 2 中 的 “ BMI 值 ” 图表 . 33 。” 但是每当我运行它时我都会遇到错误。

这是代码:

import java.util.*;
public class HealthProfile {

String firstName;
String lastName;
char gender;
int BirthMonth;
int BirthDay;
int BirthYear;
int height;
int weight;

public HealthProfile(String fName, String lName, char Genderr, int birthMonth, int birthDay, int birthYear, int heightt, int weightt){
    firstName = fName;
    lastName = lName;
    gender = Genderr;
    BirthMonth = birthMonth;
    BirthDay = birthDay;
    BirthYear = birthYear;
    height = heightt;
    weight = weightt;



}

    HealthProfile() {

    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public char getGender() {
        return gender;
    }

    public void setBirthMonth(int BirthMonth) {
        this.BirthMonth = BirthMonth;
    }

    public int getBirthMonth() {
        return BirthMonth;
    }

    public void setBirthDay(int BirthDay) {
        this.BirthDay = BirthDay;
    }

    public int getBirthDay() {
        return BirthDay;
    }

    public void setBirthYear(int BirthYear) {
        this.BirthYear = BirthYear;
    }

    public int getBirthYear() {
        return BirthYear;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public double getHeight() {
        return height;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public double getWeight() {
        return weight;
    }

public int Age(){
Calendar now = Calendar.getInstance();
int nowYear = now.get(Calendar.YEAR);
int nowMonth = now.get(Calendar.MONTH);
int nowDay = now.get(Calendar.DATE);
int day = now.get(Calendar.DATE);
int month = now.get(Calendar.MONTH);
int year = now.get(Calendar.YEAR);
if (nowMonth > BirthMonth);
return (nowYear - BirthYear);
    }

public double getBMI(){
    return (weight * 703)/(height * height);
}

public int MaxHeartRate(){
 return 220-Age();
}

public double TargetHeartRate(){
return MaxHeartRate() * 0.85 + MaxHeartRate() * 0.5;
 }
}

这是测试部分:

import java.util.Scanner;

public class HealthProfileTest {
public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
        String firstName;
        String lastName;
        String DoB;
        String theMonth;
        String theDay;
        String theYear;
        char gender;
        int Month;
        int Day;
        int Year;
        double height = 0.0;
        double weight;
       HealthProfile personalInfo = new HealthProfile();
       System.out.println("Enter your first name: ");
       firstName = input.nextLine();
       System.out.println("Enter your last name: ");
       lastName = input.nextLine();
       System.out.println("Male or female: ");
       gender = input.nextLine().charAt(0);
       System.out.println("Enter your date of birth in mm/dd/yyyy format: ");
       DoB = input.nextLine();
       theMonth = DoB.substring(0,2);
       theDay = DoB.substring(3,5);
       theYear = DoB.substring(6,10);
       Month = Integer.parseInt(theMonth);
       Day = Integer.parseInt(theDay);
       Year = Integer.parseInt(theYear);
       System.out.println("Enter your height in inches: ");
       height = input.nextInt();
       System.out.println("Enter your weight in pounds: ");
       weight = input.nextInt();
       System.out.println("Name: " + personalInfo.getFirstName() + personalInfo.getLastName());
       System.out.println("Gender: " + personalInfo.getGender());
       System.out.println("DoB: " + personalInfo.getBirthMonth() + "/" + personalInfo.getBirthDay() + "/" + personalInfo.getBirthYear());
       System.out.println("Height: " + personalInfo.getHeight());
       System.out.println("Weight: " + personalInfo.getWeight());
       System.out.println("Age: " + personalInfo.Age());
       System.out.println("BMI: " + personalInfo.getBMI());
       System.out.printf("Max heart rate: ", personalInfo.MaxHeartRate());
       System.out.printf("Target heart rate: ", personalInfo.TargetHeartRate());
       System.out.println(" ");
       System.out.println( "BMI VALUES" );
        System.out.println("Underweight: Under 18.5");
        System.out.println("Normal: 18.5-24.9 ");
        System.out.println("Overweight: 25-29.9");
        System.out.println("Obese: 30 or over");
    }
    }

这是输出:

Name: nullnull
Gender: 
DoB: 0/0/0
Height: 0.0
Weight: 0.0
Age: 2013
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at HealthProfile.getBMI(HealthProfile.java:108)
        at HealthProfileTest.main(HealthProfileTest.java:43)
Java Result: 1

我知道我做的一切都是正确的,但就是不明白为什么会这样。

4

3 回答 3

2

您忘记调用方法setHeightsetWeight. 因此,这些值仍然0是默认的,因为它们是数字原始类型。显然 2 个零值的乘积等于0然后除以0产生一个ArithmeticException

在接受输入后尝试调用方法。

System.out.println("Enter your height in inches: ");
height = input.nextInt();
personalInfo.setHeight((int) height);
System.out.println("Enter your weight in pounds: ");
weight = input.nextInt();
personalInfo.setWeight((int) weight);

同样设置“出生”字段

personalInfo.setBirthMonth(Month);
personalInfo.setBirthDay(Day);
personalInfo.setBirthYear(Year);

另外:Java 命名约定表明变量以小写字母开头,例如day,monthyear. 在此处阅读有关它们的信息

于 2013-09-06T23:43:22.453 回答
1

你的例外清楚地表明Exception in thread "main" java.lang.ArithmeticException: / by zero

正如您在自己的输出中看到的那样,height为 0,因此当您获得 BMI 时,您将除以 0,这在以下位置是不合法的操作:

public double getBMI(){
    return (weight * 703)/(height * height);
}

确保完全运行程序并输入有效高度。

此外,正如@jlordo 指出的那样,您的代码正在进行整数除法(因为所涉及的所有值都是整数。这将使您在小数点后丢失任何内容。尝试使用:

public double getBMI(){
    return ((double)weight * 703)/(height * height);
}

反而。将其中一个涉及的值转换为 adouble会使 Java 保留十进制值。

于 2013-09-06T23:38:37.787 回答
0

在您的以下方法中,将其更改为如下所示:

public double getBMI(){
   if(height == 0)
       return (weight * 703)/(height * height);
   else
       return x;   // x is whatever the value you want
}
于 2013-09-06T23:41:07.040 回答