似乎无法返回价值

时间:2013-04-09 18:40:01

标签: java class object

我刚刚编辑了我的帖子以更新我的代码...当我编译声明“无法找到符号”时,我仍然收到错误消息。我从未见过这个错误所以我不确定如何调试它,我非常感谢大家的帮助!

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


} 

2 个答案:

答案 0 :(得分: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以遵循方法命名约定。

答案 1 :(得分: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());