我刚刚编辑我的帖子以更新我的代码...编译时仍显示一条错误消息,提示“找不到符号”。我从未见过此错误,所以我不确定如何调试它,非常感谢大家的帮助!
public class HealthRecord {
private int ID; // stores ID number
private String last; // stores last name
private String first; //stores first name
private double height; // stores height
private double weight; // stores weight
private double bmi;// i may need to create the bmi variable so that it can be stored (weight / (height*height)) *703;
public HealthRecord( int ID, String last, String first, double height, double weight)
{
this.ID = ID;
this.last = last;
this.first = first;
this.height = height;
this.weight = weight;
this.bmi = bmi;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
public String getFirst() {
return first;
}
public void setFirst(String last) {
this.first = first;
}
public double getheight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getbmi() {
return (weight / (height*height)) *703;
}
public void setbmi(double bmi) {
this.bmi = bmi;
}
}
下一班
import java.util.Scanner;
import java.io.File;
public class OpenFile
{
private Scanner input;
public void openFile() throws Exception
{
input = new Scanner(new File("medrec.txt")); // opens the text file to be read (weight / (height*height)) *703);
}
public void readFile()
{
int ID; // stores ID number
String last; // stores last name
String first; //stores first name
double height; // stores height
double weight; // stores weight
input.nextLine();
while(input.hasNextLine())
{
ID = input.nextInt();
last = input.next();
first = input.next();
height = input.nextDouble();
weight = input.nextDouble();
HealthRecord healthrecord= new HealthRecord(ID,last,first,height,weight);
System.out.printf("%d", healthrecord.getID(), healthrecord.getBmi());
}
}
public void closeFile()
{
input.close();
}
}
最佳答案
更换
System.out.printf("%d %d", HealthRecord.getID, HealthRecord.getBmi);
与
System.out.printf("%d %f", healthrecord.getID(), healthrecord.bmi());
使用您创建的
healthrecord
实例。方法getID
需要括号。同样,getBmi
也不存在。当前您有bmi
。在旁边:
目前,此代码无法编译。
openFile
中的所有方法都是实例方法。您需要创建一个实例并使用:Openfile myOpenfile = new Openfile();
myOpenfile.openFile();
myOpenfile.readFile();
myOpenfile.closeFile();
Java命名约定表示类名以大写字母开头,例如
Openfile
编辑:
在更新的文件中,
getBmi
不存在,而是getbmi
:System.out.printf("%d %f", healthrecord.getID(), healthrecord.getbmi());
^
您可能需要将方法重命名为
getBmi
以遵循方法命名约定。