基本Java,OOP

时间:2016-02-07 23:35:57

标签: java oop

我现在卡住了,我的程序应该正常工作,除非它为 BMI输出返回 0.0 的值,我确定它的东西是我忽略了,但我似乎无法弄明白。 我在另一个问题上发布了相同的代码,但是现在解决了一个不同的问题。以下是类代码...任何指针都表示赞赏!我还没有接受calCompu方法,我想先让resultBMI工作......

import java.util.Scanner;

public class myPerson 
{
double weight;
double height;
int age;
String gender;
double calCompu;
final double KG_WEIGHT_CONV = 2.2;   
final double MT_HEIGHT_CONV = 0.0254;

public myPerson ()
{
weight = 0;
height = 0;
age = 0;
gender = " ";
}

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

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

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

public double getWeight () {
    return weight / KG_WEIGHT_CONV;     
}

public double getHeight () {
    return height * MT_HEIGHT_CONV;     
}


public int getAge () {
    return age;     
}

public String getGender () {
    return gender;     
}

//instance method to calculate BMI
public double bodyMassIndex()
{ 

double resultBMI = weight / Math.pow(height, 2);

return resultBMI;
}  

public double calorieComp(double weight, double height, int age)
{  
    double heightInCentimeters = height * 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: ");

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

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

    if ("m".equals(gender))
    {

    //return men
    double basalMetabolicRate = 13.397 * weight + 4.799 
            * heightInCentimeters - 5.677 * age + 88.362;

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

    return calCompu;
    }

    else
    {

    //return women
    double basalMetabolicRate = 9.247 * weight + 3.098 
            * heightInCentimeters - 4.330 * age + 447.593;

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

    return calCompu;
    }      
}
}

主要班级

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 = keyboard.next();

    //person.bodyMassIndex(0, 0);
   // person.calorieComp(0, 0, 0);
    double resultBMI = person.bodyMassIndex();
    displayResults(person, resultBMI, 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(myPerson person, double resultBMI, double calCompu)
{

    Scanner keyboard2 = new Scanner( System.in );
    final double LOW_N_BMI = 18.5;
    final double HI_N_BMI = 24.9;

    System.out.println("\nRESULTS:");

    System.out.printf("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("\n\nCurrent weight in kgs is: " + "%.2f", person.getWeight());
    //System.out.printf("\nCurrent height in meteres: " + "%.3f", person.getHeight());
    if ("m".equals(person.gender))
        System.out.printf("\nYou are a " + "Male,");
    else
        System.out.printf("\nYou are a " + "Female,");
    System.out.printf(" age " + person.age);
    System.out.printf(", with a weight of " + "%.2f", person.getWeight());
    System.out.printf(" kg, a height of " + "%.3f", person.getHeight());
    System.out.printf(" m.");



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

    System.out.printf("\n To 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 ("y".equals(questionOne))
        {
            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
    {       
    }

}  
}

示例输入/输出

run:
This program implements a Health Assistance Calculator

Given a weight, height, and age, it will compute:
    BMI - Body Mass Index
    Calories needed per day to maintain weight

Please enter weight in pounds: 180
Please enter height in inches: 68
Please enter your age in years: 24
Please enter your gender (M/F): m
Select your activity level: 
  1 - Sedentary
  2 - Moderately active (light exercise 1-3 days a week) 
  3 - Active (moderate exercise 3-5 days a week) 
  4 - Very active (heavy exercise 6-7 days a week) 
Enter choice from above: 2

RESULTS:
BMI in the range of 18.5 to 24.9 is considered normal
You are a Male, age 24, with a weight of 81.82 kg, a height of 1.727 m.
Your BMI is 0.0, which is below normal.

To maintain your current weight, you should consume Would you like to try and reach the normal range? 
y
how many pounds would you like to gain per week? 
5
To gain 5.0 pound(s) per week, you should consume 17500.0Calories per day.
BUILD SUCCESSFUL (total time: 22 seconds)

2 个答案:

答案 0 :(得分:0)

问题是重量和高度单位之间的不匹配。你的体重是英镑,身高是英寸。在你的BMI公式中插入145磅和69英寸,你得到145 / 69^2~ 0.03 BMI。您在输出中看到0.0,因为占位符"%.1"将输出限制为小数点后的一位数。

Modify美国单位的BMI公式。

答案 1 :(得分:0)

我知道它必须是一件我想念的简单...谢谢@Stiliyan它现在处于正常工作状态,现在让我们看看我是否可以让其余部分工作。哈哈,你们真棒!

public double bodyMassIndex()
{ 
    double weightKg = weight / KG_WEIGHT_CONV;
    double heightMeters = height * MT_HEIGHT_CONV;
    double resultBMI = weightKg / Math.pow(heightMeters, 2);

    return resultBMI;
}