-1

プログラムに問題があります。あなたがそれを入力したり年齢を与えたりするとき、それはDoBを表示しません。また、最大心拍数や目標心拍数も表示されません。私が何を間違えたか知っている人はいますか?

コーディングは次のとおりです。

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);
int Age = (nowYear-BirthYear);
return Age;
    }

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

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

public double getTargetHeartRate(){
return getMaxHeartRate() * 0.85 + getMaxHeartRate() * 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 = 0;
        int day = 0;
        int year = 0;
        double height;
        double weight;
       HealthProfile personalInfo = new HealthProfile();
       System.out.println("Enter your first name: ");
       personalInfo.setFirstName(input.nextLine());
       System.out.println("Enter your last name: ");
       personalInfo.setLastName(input.nextLine());
       System.out.println("Male or female: ");
       personalInfo.setGender(input.nextLine().charAt(0));
       System.out.println("Enter your date of birth in mm/dd/yyyy format: ");
       DoB = input.nextLine();
       personalInfo.setBirthMonth(month);
       personalInfo.setBirthDay(day);
       personalInfo.setBirthYear(year);
       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.getMaxHeartRate());
       System.out.print(" ");
       System.out.printf("Target heart rate: ", personalInfo.getTargetHeartRate());
       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");
    }
    }

出力 (機能していない結果のみを表示しています):

DoB: 0/0/0
Age: 2013
Max heart rate:
Target heart rate: 
4

2 に答える 2

3

については、 、および を実際に計算する前に属性値DoBを設定しています。これを変える :HealthProfiledaymonthyear

   //month,day and year are not properly calcualted yet
   personalInfo.setBirthMonth(month);
   personalInfo.setBirthDay(day);
   personalInfo.setBirthYear(year);
   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);

   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);
   //month,day and year now have a proper value, so set it in your personalInfo 
   personalInfo.setBirthMonth(month);
   personalInfo.setBirthDay(day);
   personalInfo.setBirthYear(year);
于 2013-09-07T12:32:23.917 に答える
0

あなたは正しく印刷していません:

System.out.printf("Max heart rate: ", personalInfo.getMaxHeartRate());
System.out.printf("Target heart rate: ", personalInfo.getTargetHeartRate());

次のようにする必要があります。

System.out.printf("Max heart rate: %d", personalInfo.getMaxHeartRate());
System.out.printf("Max heart rate: %.1f", personalInfo.getTargetHaertRate());
于 2013-09-07T12:34:10.410 に答える