对于我的课程,我必须编写一个提示输入此人信息的java应用程序,为该人员实例化一个健康档案的对象并打印该对象的信息 - 包括该人的名字,姓氏,性别,出生日期,身高和体重 - 然后计算和打印人的年龄,体重指数,最大心率和目标 - 心率范围。它还应显示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");
}
}
最后,这是输出。我的名字没有显示,我的性别,出生日期和我的年龄都没有显示2015年。如果您有任何意见,我们将不胜感激。
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
更新:我修正了名称问题。但我的性别和年龄仍然偏远。感谢你目前的帮助。
答案 0 :(得分:0)
您永远不会调用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);
你对长度和其他方法做了什么。
答案 1 :(得分:0)
尝试添加
personalInfo.setFirstName(firstName);
这两行之后
System.out.println("Enter your first name: ");
firstName = input.nextLine();
和LastName
personalInfo.setLastName(lastName);
因为除了您将设置引用变量firstName
和lastName
之外,它们的默认值将保留为空{默认值instance
和static
参考变量为空。
这就是你的输出为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方法
来获取它们