Java 找不到符号(在初始化新类对象时指向“new”?
对于家庭作业,我有一个“健康”或基本上是一个BMI/BMR计算器。当我尝试使用之前在我的Tester类中由扫描器获取的值创建一个新的类对象时,它告诉我找不到符号。我觉得这似乎是一件显而易见的事情,但与此同时,我在做一件像创建一个新对象这样的普通事情时遇到了一个错误,这一事实让我感到困惑 编辑:也不确定如何最好地粘贴或指出错误发生的行,但它在测试仪代码上已经完成了一半。我试图加粗,但我不想打破滚动框 实际误差:Java 找不到符号(在初始化新类对象时指向“new”?,java,Java,对于家庭作业,我有一个“健康”或基本上是一个BMI/BMR计算器。当我尝试使用之前在我的Tester类中由扫描器获取的值创建一个新的类对象时,它告诉我找不到符号。我觉得这似乎是一件显而易见的事情,但与此同时,我在做一件像创建一个新对象这样的普通事情时遇到了一个错误,这一事实让我感到困惑 编辑:也不确定如何最好地粘贴或指出错误发生的行,但它在测试仪代码上已经完成了一半。我试图加粗,但我不想打破滚动框 实际误差: HowHealthy.java:87: cannot find symbol symb
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
^
import java.util.Scanner;
/*
*测试健康班级的班级
*/
公共课如何健康
{
公共静态void main(字符串[]args)
{
//声明
字符串名;
字符串性别;
性别状态;
双倍重量;
字符串权重状态;
双高;
智力年龄;
智力水平;
//创建健康对象
健康测试=新的健康测试();
//获取并验证信息
扫描仪扫描=新扫描仪(System.in);
//输入名称
系统输出打印(“人名:”);
字符串nameScan=scan.nextLine();
如果(nameScan.length()>0)
{
name=nameScan;
}
其他的
{
System.out.println(“错误:名称必须至少包含一个字符”);
系统出口(0);
}
//输入性别
System.out.printf(“\n%s,您是男性还是女性(男/女):”,名称扫描);
char genderScan=scan.nextLine().toUpperCase().charAt(0);
if((genderScan='M')| |(genderScan='F'))
{
genderStatus=genderScan;
}
其他的
{
System.out.println(“错误:性别必须为M或F”);
系统出口(0);
}
//输入重量
System.out.printf(“\n%s的重量(磅):”,名称扫描);
double weightScan=scan.nextDouble();
如果(权重扫描>=100)
{
重量=重量扫描;
}
其他的
{
System.out.println(“错误:重量必须至少为100磅”);
系统出口(0);
}
//输入高度
System.out.printf(“\n%s的高度(英寸):”,名称扫描);
双高扫描=扫描.nextDouble();
如果(高度扫描>=60&&heightScan=18)
{
年龄=年龄扫描;
}
其他的
{
System.out.println(“错误:必须至少有18年历史”);
系统出口(0);
}
//使用从扫描仪声明的值创建另一个运行状况对象
**Healthy healthObj1=新健康人(姓名、性别、体重、身高、年龄)**
System.out.println(“\n活动级别:使用这些类别:”);
System.out.println(“\t1-久坐(很少或没有锻炼,办公桌工作)”);
System.out.println(“\t2-轻度活动(少量运动/运动3-5天/周”);
System.out.println(“\t3-适度运动(适度运动/运动3-5天/周)”);
System.out.println(“\t4-非常活跃(剧烈运动/运动6-7天/周)”;
System.out.println(“\t5-额外活动(日常艰苦锻炼/体育锻炼工作或2天训练,如马拉松、竞赛等”);
System.out.print(“\n\n您是如何活动的?”);
int levelTemp=scan.nextInt();
/*验证级别输入*/
如果((levelTemp>=1)&&(levelTemp=18.5)&&&(healthObj1.getBMI(体重、身高)<25))
{
weightStatus=“正常重量”;
}
否则如果((healthObj1.getBMI(体重、身高)>=25)和&(healthObj1.getBMI(体重、身高)<30))
{
weightStatus=“超重”;
}
否则如果((healthObj1.getBMI(体重、身高)>=30))
{
weightStatus=“肥胖”;
}
System.out.println(“你的体重指数将你分类为”+体重状态);
}//结束类main
}//结束类
^
您的构造函数需要6个参数,而您只传递了5个参数(缺少inActLevel:))
而:
public Healthy(String inName, char inGender, double inWeight, double inHeight, int inAge, int inActLevel) {...}
编辑:你错过了健康类中的getter和setter,但我不知道这是否是因为你没有粘贴所有内容^ ^是的,我之前尝试过获取帮助,因此一位同学将他们的代码发送给我参考,但我不确定他们为什么创建了一个新的健康对象(healthyObj1)?我仍在尝试计算我最初的healthTest对象的信息,但他们让它听起来像他们的代码在工作。也许不是。如果我坚持他的路径并输入inActLevel,我得到的所有变量可能都没有初始化错误。这对我来说很有意义。我可能不得不从另一个方向再试一次。
public Healthy(String inName, char inGender, double inWeight, double inHeight, int inAge, int inActLevel) {...}