调用类方法并显示结果

时间:2016-02-07 17:38:39

标签: java oop if-statement

更新后作为修正

问题我现在正在让displayResults方法调用体重高度和性别进行计算。另外我还有一个问题,我需要用户输入来忽略响应的情况。

import java.util.Scanner;

public class myHealthCalculator 
{    
public static void main(String[] args) 
{   
    displayPurpose();

    //create person object
    myPerson person = new myPerson();       

    Scanner keyboard = new Scanner( System.in );

    System.out.print("Please enter weight in pounds: ");
    person.weight = keyboard.nextDouble();

    System.out.print("Please enter height in inches: ");
    person.height = keyboard.nextDouble();

    System.out.print("Please enter your age in years: ");
    person.age = keyboard.nextInt();

    System.out.print("Please enter your gender (M/F): ");
    person.gender = (char) keyboard.nextDouble();

    person.bodyMassIndex(0, 0);
    person.calorieComp(0, 0, 0);
displayResults(person.bodyMassIndex(0, 0),person.calorieComp(0, 0, 0));
}

public static void displayPurpose()
{
    System.out.println("This program implements a Health Assistance "
            + "Calculator");
    System.out.println("\nGiven a weight, height, and age, it will compute:");
    System.out.println("    BMI - Body Mass Index");
    System.out.println("    Calories needed per day to maintain weight");
    System.out.println();
}   

public static void displayResults(double resultBMI, int calCompu)
{
    Scanner keyboard2 = new Scanner( System.in );
    final double LOW_N_BMI = 18.5;
    final double HI_N_BMI = 24.9;

    System.out.println("\n");

    System.out.printf("%nA BMI in the range of " + LOW_N_BMI + " to " 
            + HI_N_BMI + " is considered normal" );
    //getters here, gender, age, weight kgs and height meters.
    System.out.printf("Current weight in kgs is: " + "%.2f", weight);
    System.out.printf("Current height in meteres: " + "%.3f", height);
    if (gender == 'm')
        System.out.println("Gender is: " + "Male");
    else
        System.out.println("Gender is: " + "Female");

    System.out.printf("Your BMI is: " + "%.1f", resultBMI);
    if (resultBMI < LOW_N_BMI)
        System.out.println("BMI is below normal");
    else if (resultBMI > HI_N_BMI)
        System.out.println("BMI is above normal");
    else
        System.out.println("BMI is normal");

    System.out.printf("%nTo maintain your current weight, you should consume ", calCompu, "calories per day.");        

    if (resultBMI < LOW_N_BMI)
    {
        System.out.println("Would you like to try and reach the normal range? ");
        String questionOne = keyboard2.next();
        if (questionOne == "y")
        {
            System.out.println("how many pounds would you like to gain per week? ");
            double questionTwo = keyboard2.nextDouble();
            double gainRate = questionTwo * 3500;
            System.out.println(" To gain " + questionTwo + " pound(s) per "
                    + "week, you should consume " + gainRate + "Calories per day.");
        }
        else
        {

        }
    }
    else if (resultBMI > HI_N_BMI)
    {
        System.out.println("Would you like to try and reach the normal range? ");
        double questionThree = keyboard2.nextDouble();
        double loseRate = questionThree / 3500;
        System.out.println(" To lose " + questionThree + " pound(s) per "
                    + "week, you should consume " + loseRate + "Calories per day.");
    }
    else
    {       
    }

}  
}

和另一个班级

import java.util.Scanner;

public class myPerson 
{
// data fields
double weight = 0;
double height = 0;
int age = 0;
double gender = ' ';  //char 'M' or 'F'
private double resultBMI = 0;

//person constructor
public myPerson ()
{
    //conversion factors for metric (need to convert input 
    //which will be in imperial messurements)
    final double KG_WEIGHT_CONV = 2.2;   
    final double MT_HEIGHT_CONV = 0.0254;
    double calCopu = 0.0;
}

public void setWeight (double weight, double KG_WEIGHT_CONV) {
    this.weight = weight / KG_WEIGHT_CONV;     
}

public void setHeight (double height, double MT_HEIGHT_CONV) {
    this.height = height * MT_HEIGHT_CONV;     
}
public void setAge (int age) {
    this.age = age;     
}

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


public double getWeight () {
    return weight;     
}

public double getHeight () {
    return height;     
}


public int getAge () {
    return age;     
}

public double getGender () {
    return gender;     
}

//instance method to calculate BMI
public double bodyMassIndex(double weightKgs, double heightMeters)
{ 
    /**
     * Compute a person's BMI, will return a double value
     * squares the height using the pow method from java.Math.
    */
    return (weightKgs / Math.pow(heightMeters, 2));
}  

public int calorieComp(double weightInKg, double heightInMeters, int ageInYears)
{  
    double heightInCentimeters = heightInMeters * 100;

    System.out.println("Select your activity level: ");
    System.out.println("  1 - Sedentary");
    System.out.println("  2 - Moderately active (light exercise 1-3 days a week) ");
    System.out.println("  3 - Active (moderate exercise 3-5 days a week) ");
    System.out.println("  4 - Very active (heavy exercise 6-7 days a week) ");
    System.out.printf("Enter choice from above: ");

    char aFactor;
    Scanner keyboard = new Scanner(System.in);

    aFactor = keyboard.next().charAt(0);
    switch (aFactor)
    {
        case '1':
            aFactor = (char) 1.2;  //Sedentary people
            break; 
        case '2':
            aFactor = (char) 1.375;    //Moderate active people
            break;
        case '3':
            aFactor = (char) 1.55; //Active people
            break;
        case '4':
            aFactor = (char) 1.725;    //Very active people
            break;
        default :
            System.out.println("Invalid choice entered!");
    }

    if (gender == 'm')
    {

    //return men
    double basalMetabolicRate = 13.397 * weightInKg + 4.799 
            * heightInCentimeters - 5.677 * ageInYears + 88.362;

    double psyActFact = basalMetabolicRate * aFactor;
    int calCompu = (int) Math.ceil(psyActFact);

    return calCompu;
    }

    else
    {

    //return women
    double basalMetabolicRate = 9.247 * weightInKg + 3.098 
            * heightInCentimeters - 4.330 * ageInYears + 447.593;

    double psyActFact = basalMetabolicRate * aFactor;
    int calCompu = (int) Math.ceil(psyActFact);

    return calCompu;
    }      
}
}

1 个答案:

答案 0 :(得分:0)

所以我想了解更多并希望分享,我将displayResults方法更改为

displayResults(person, person.bodyMassIndex(0, 0),person.calorieComp(0, 0, 0));

并在main方法中将displayResults调用更改为

displayResults(person, person.bodyMassIndex(0, 0),person.calorieComp(0, 0, 0));

我还从main方法中删除了两行代码,导致程序要求两次活动级别。

现在允许数据移动,我得到一些结果返回。 然而,它给出了 NaNBMI 的结果,因为“你的BMI是:”还需要让它将它转换为米和kgs ...但是我已经取得了进展,我会发布如果我再取得进展,欢迎任何更多的建议= D