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
^
是的,我試圖得到這個幫助,所以一個同學送我他們的代碼引用,但我不確定他們爲什麼創建一個新的健康對象無論如何(healthyObj1)?我仍然試圖計算我的原始healthTest對象的信息,但他們聽起來像他們的代碼正在工作。也許不是。 如果我堅持他的道路,並放入inActLevel我得到所有變量可能沒有被初始化錯誤。這對我有意義。我可能不得不從另一個方向嘗試它。 – Todd 2010-11-04 02:31:59