Java I';我有输出错误。有人能解释一下我';你做错了什么?

Java I';我有输出错误。有人能解释一下我';你做错了什么?,java,Java,对于我的类,我必须编写一个java应用程序,提示输入个人信息,实例化该个人的类健康档案对象,并打印来自该对象的信息-包括该个人的名字、姓氏、性别、出生日期,身高和体重-然后计算并打印该人的年龄(以年为单位)、BMI、最大心率和目标心率范围。它还应显示BMI值 如果您查看输出,您可以看到代码的哪一部分经过了处理。我完全被难住了。我在java方面还处于初级阶段 以下是代码的第一部分,Healthprofile: import java.util.*; public class HealthProf

对于我的类,我必须编写一个java应用程序,提示输入个人信息,实例化该个人的类健康档案对象,并打印来自该对象的信息-包括该个人的名字、姓氏、性别、出生日期,身高和体重-然后计算并打印该人的年龄(以年为单位)、BMI、最大心率和目标心率范围。它还应显示BMI值

如果您查看输出,您可以看到代码的哪一部分经过了处理。我完全被难住了。我在java方面还处于初级阶段

以下是代码的第一部分,Healthprofile:

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;
}
}
这是第二部分,HealthProfileApp部分:

import java.util.Scanner;

    public class HealthProfileApp {
    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();
   personalInfo.setHeight((int) height);
   System.out.println("Enter your weight in pounds: ");
   weight = input.nextInt();
   personalInfo.setWeight((int) weight);
   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: 69.0
Weight: 125.0
Age: 2015
BMI: 18.0
Max heart rate: Target heart rate:
BMI VALUES
Underweight: Under 18.5
Normal: 18.5-24.9
Overweight: 25-29.9
Obese: 30 or over

更新:我修复了名称问题。但我的性别和年龄仍然相去甚远。谢谢你迄今为止的帮助

您从不调用setfirstname方法

您可以让用户输入他们的名字和姓氏。然后你什么也不做。因此,当调用getfirstname和getlastname方法时,它将返回null

更新

在这里,您可以让用户输入名字和姓氏:

 System.out.println("Enter your first name: ");
   firstName = input.nextLine();
   System.out.println("Enter your last name: ");
   lastName = input.nextLine();
您必须添加以下内容:

personalInfo.setFirstName(firstName);
personalInfo.setLastName(lastName);
与长度和其他方法完全相同。

尝试添加

personalInfo.setFirstName(firstName);
在这两行之后

  System.out.println("Enter your first name: ");
   firstName = input.nextLine();
LastName

personalInfo.setLastName(lastName);
因为直到并且除非您将参考变量设置为:
firstName
lastName
,否则这些变量的默认值将保持为null,因为
实例
静态
参考变量的默认值为null

这就是您的输出为null的原因


要计算年龄,您可以使用第三方库JODA

int month=Integer.parseInt(theMonth);
int day= Integer.parseInt(theDay);
int year=Integer.parseInt(theYear);

LocalDate birthdate = new LocalDate (year,month,day); //Birth date
LocalDate now = new LocalDate();                    //Today's date
Period period = new Period(birthdate, now, PeriodType.yearMonthDay());

//Now access the values as below
System.out.println(period.getDays());
System.out.println(period.getMonths());
System.out.println(period.getYears());

现在,您可以根据上述变量设置年龄,然后尝试使用getters方法获取它们

您不应该给变量命名以大写字母开头。它不会阻止您的代码编译,但这在文化上很重要。请不要给我的变量命名以大写字母开头?像我的字符串变量?任何变量。e、 g.出生月份;这被认为是不好的。使用int生日月份;相反,我以为我做到了?我需要在哪里调用此方法?我很抱歉提出这些问题,我刚刚花了几个小时来讨论这件事,我觉得这件事太离谱了。编辑:我在HealthProfile的第32行调用了setFirstName。我使用set和get方法调用了First和Last?您已经声明了这些方法,但从未使用过它们,请参见我的带日期的答案,只需将这两个方法添加到lastName=input.nextLine()下即可;方法:)我做到了!而且它找不到符号。所以它没有足够的信息来处理代码。@DarkFireInashency性别也是一样,用户输入性别,但您不调用set-gender方法。非常感谢Johan。现在我只是在设定我的出生日期和正确的年龄方面遇到了困难。你们用我理解的术语解释事情,我很感激。好吧,我之前试过这个,它说“错误:找不到符号”,所以我不知道它指的是什么符号。我把它添加到第21行,就在你说的地方。你能发布整个堆栈跟踪以便我们能更好地帮助你吗?当你说整个堆栈跟踪时,你指的是什么?你得到的完整错误,java异常肯定会指出编译器找不到的符号我实际上修复了名称问题。我现在的问题是,我的性别和年龄都不正确。