0

我刚刚编辑了我的帖子以更新我的代码......当我编译指出“找不到符号”时,我仍然收到一条错误消息。我从来没有见过这个错误所以我不知道如何调试它,非常感谢大家的帮助!

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(); 
    } 


} 
4

2 回答 2

1

代替

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为遵循方法命名约定。

于 2013-04-09T18:44:02.993 回答
0

我在HealthRecord构造函数或 bmi 的 getter 方法中看不到任何 bmi 计算。即使我在课堂上也看不到getBmi()方法。HealthRecord

在类中添加以下方法HealthRecord

public double getBmi() {

    return (weight / (height*height)) *703; //check this formula if it is correct for BMI calculation or not
}

接下来你需要在主类中使用这个调用:

System.out.printf("%d %d", healthrecord.getID(), healthrecord.getBmi());
于 2013-04-09T18:56:24.400 回答