Basic Java: method calculation returns NaN -
i'm learning java having difficulty creating method calculates body mass index. doesn't accept instance variables declared earlier in program , i'm not sure how fix this. appreciated!
code:
import java.util.formatter; public class healthrecord { // initialise instance variables string first; string last; double height; double weight; public healthrecord(string lastname, string firstname, double height, double weight) { first = firstname; last = lastname; height = height; weight = weight; } public static void main(string args[]) { healthrecord record = new healthrecord("last", "first", 72, 150); system.out.println("bmi="+record.getbmi()); } // calculate bmi public double getbmi() { double bmi = weight / (height * height) * 703.0; return bmi; } } result: bmi=nan
update constructor below:
public healthrecord(string lastname, string firstname, double height, double weight) { first = firstname; last = lastname; this.height = height; this.weight = weight; }
Comments
Post a Comment