2010-11-04 52 views
0

对于家庭作业,我有一个“健康”或基本上是BMI/BMR计算器。当我尝试使用先前在我的Tester类中拾取的值创建一个新的类对象时,它告诉我找不到符号。我感觉好像是明显的东西,但同样的事实是,我得到一个错误,做一个像创建一个新对象这样的常见事物正在困扰着我。找不到符号(指向新类对象启动时的“新”)?

编辑:也不知道如何最好地粘贴或指出错误发生的地方,但它在测试器代码上的一半。我试图大胆,但我不想打破滚动框。

实际误差:

HowHealthy.java:87: cannot find symbol 
symbol : constructor Healthy(java.lang.String,char,double,double,int) 
location: class Healthy 
    Healthy healthObj1 = new Healthy(name, genderStatus, weight, height, age); 

类:

public class Healthy 
{ 
// define attributes 
private String name; 
private char gender; 
private double weight; 
private double height; 
private double weightMetric; 
private double heightMetric; 
private int age; 
private double finalBMR; 
private double finalBMI; 
private double finalTDEE; 
private final double CENTIMETER_CONVERSION = 2.54; 
private final double KILOGRAM_CONVERSION = 0.45359237; 


/* Creates constructor with default values 
*/ 
public Healthy() 
{ 
    name = ""; 
    weight = 0.0; 
    height = 0.0; 
    age = 0; 
} 

/* Creates constructor with initialized values 
*/ 
public Healthy(String inName, char inGender, double inWeight, double inHeight, int inAge, int inActLevel) 
{ 
    name = inName; 
    gender = inGender; 
    weight = inWeight; 
    height = inHeight; 
    age = inAge; 
} 

} // end class Healthy 

import java.util.Scanner; 
/* 
* A class to test the Healthy Class 
*/ 

public class HowHealthy 
{ 
public static void main (String[] args) 
{ 
    //Declarations 
    String name; 
    String gender; 
    char genderStatus; 
    double weight; 
    String weightStatus; 
    double height; 
    int age; 
    int level; 

    //Create Health object 
    Healthy healthTest = new Healthy(); 

    //Get and validate info 
    Scanner scan = new Scanner(System.in); 
    //Enter name 
    System.out.print("Person's name: "); 
    String nameScan = scan.nextLine(); 
    if (nameScan.length() > 0) 
    { 
    name = nameScan; 
    } 
    else 
    { 
    System.out.println("ERROR: Name must include at least one character"); 
    System.exit(0); 
    } 
    //Enter gender 
    System.out.printf("\n%s, are you male or female (M/F): ", nameScan); 
    char genderScan = scan.nextLine().toUpperCase().charAt(0); 
    if ((genderScan == 'M') || (genderScan == 'F')) 
    { 
    genderStatus = genderScan; 
    } 
    else 
    { 
    System.out.println("ERROR: Gender must be either M or F"); 
    System.exit(0); 
    } 
    //Enter weight 
    System.out.printf("\n%s's weight (pounds): ", nameScan); 
    double weightScan = scan.nextDouble(); 
    if (weightScan >= 100) 
    { 
    weight = weightScan; 
    } 
    else 
    { 
    System.out.println("ERROR: Weight must be at least 100 pounds"); 
    System.exit(0); 
    } 
    //Enter height 
    System.out.printf("\n%s's height (inches): ", nameScan); 
    double heightScan = scan.nextDouble(); 
    if (heightScan >= 60 && heightScan <= 84) 
    { 
    height = heightScan; 
    } 
    else 
    { 
    System.out.println("ERROR: Height must be between 60 to 84 inches"); 
    System.exit(0); 
    } 
    //Enter age 
    System.out.printf("\n%s's age (years): ", nameScan); 
    int ageScan = scan.nextInt(); 
    if (ageScan >= 18) 
    { 
    age = ageScan; 
    } 
    else 
    { 
    System.out.println("ERROR: Must be at least 18 years old"); 
    System.exit(0); 
    } 

    //Creating another Health object using values declared from Scanner 
**Healthy healthObj1 = new Healthy(name, genderStatus, weight, height, age);** 


    System.out.println("\nActivity Level: Use these categories: "); 
     System.out.println("\t1 - Sedentary (little or no exercise, desk job)"); 
     System.out.println("\t2 - Lightly active (little exercise/sports 3-5 days/wk"); 
     System.out.println("\t3 - Moderately active(moderate exercise/sports 3-5 days/wk)"); 
     System.out.println("\t4 - Very active (hard exercise/sports 6 -7 day/wk)"); 
     System.out.println("\t5 - Extra active (hard daily exercise/sports physical \n\t  job or 2X day training i.e marathon, contest, etc.)"); 
     System.out.print("\n\nHow active are you? "); 
    int levelTemp = scan.nextInt(); 

    /*Validate the level input */ 
    if ((levelTemp >= 1) && (levelTemp <= 5)) 
    { 
    level = levelTemp; 
    } //if 
    else 
    { 
    System.out.println("Invalid leve - must between 1 to 5"); 
    System.exit(0); 
    } //else  



    System.out.printf("\n\n%s's information\n", name); 
    System.out.printf("Weight: %.1f pounds \n", healthObj1.getWeight()); 
    System.out.printf("Height: %.1f inches \n", healthObj1.getHeight()); 
    System.out.printf("Age: %d years \n", healthObj1.getAge()); 

    /*Give the proper syntax for gender */ 
    if (genderStatus == 'M') 
    { 
    gender = "Male"; 

    if(genderStatus =='F') 
    { 
    gender = "Female"; 
    } 

    } 

    System.out.print("These are for a " + gender + ".\n\n"); 
    System.out.printf("BMR: %.2f pounds\n", healthObj1.getBMI(weight, height)); //Pass the weight and height 
    System.out.printf("BMI: %.2f inches\n", healthObj1.getBMR(genderStatus)); //Pass the gender 
    System.out.printf("TDEE: %.2f years\n", healthObj1.getTDEE(level)); // Pass the level 

    /*Give the overall status of the weight */ 

    if ((healthObj1.getBMI(weight, height)< 18.5)) 
    { 
    weightStatus = "Underweight"; 

    } 
    else if((healthObj1.getBMI(weight, height)>= 18.5) && (healthObj1.getBMI(weight, height)< 25)) 
    { 
    weightStatus = "Normal weight"; 
    } 
    else if((healthObj1.getBMI(weight, height)>= 25) && (healthObj1.getBMI(weight, height)< 30)) 
    { 
    weightStatus = "Overweight"; 
    } 
    else if((healthObj1.getBMI(weight, height)>= 30)) 
    { 
    weightStatus = "Obese"; 
    }  

    System.out.println("Your BMI classifies you as " + weightStatus); 
} // end class main 
} // end class HowHealthy 
       ^

回答

2

你的构造函数期待6个参数,你就只有通过5( inActLevel是m发布:))。

Healthy healthObj1 = new Healthy(name, genderStatus, weight, height, age); 

虽然:

public Healthy(String inName, char inGender, double inWeight, double inHeight, int inAge, int inActLevel) {...} 

编辑:和你错过了你的健康类的getter和setter方法,但我不知道这是否是因为你没有的所有内容粘贴^^

+0

是的,我试图得到这个帮助,所以一个同学送我他们的代码引用,但我不确定他们为什么创建一个新的健康对象无论如何(healthyObj1)?我仍然试图计算我的原始healthTest对象的信息,但他们听起来像他们的代码正在工作。也许不是。 如果我坚持他的道路,并放入inActLevel我得到所有变量可能没有被初始化错误。这对我有意义。我可能不得不从另一个方向尝试它。 – Todd 2010-11-04 02:31:59