JAVA:使用BMI计算器

时间:2014-09-03 12:49:58

标签: java arraylist double

我是java的新手,试图使用构造函数,公共实例方法和toString方法构建一个简单的BMI计算器。

public class BMI {

public BMI(String name, double height, double weight){

}

public String getBMI() {
    return (weight/height);
  }


  public String toString() {
        return name + "is" + height + "tall and is " + weight +
                "and has a BMI of" + getBMI() ;
      }

public static void main(String[] args) {


}

我真的不知道我在做什么,所以所有的帮助都表示赞赏。如果你知道如何完成这个并且可以向我展示一个主要的方法,可以演示如何使用它,这将更加受到赞赏。

谢谢:)

3 个答案:

答案 0 :(得分:0)

构造函数中只有局部变量。

public class BMI {

String name;
double height, weight;

public BMI(String name, double height, double weight){
    this.name = name;
    this.height = height;
    this.weight = weight;
}

public double getBMI() {
    return (weight/height);
  }


  public String toString() {
        return name + "is" + height + "tall and is " + weight +
                "and has a BMI of" + getBMI() ;
      }

public static void main(String[] args) {
     BMI obj = new BMI("John",77,44);
     System.out.println(obj);
     //or
     double bmi = obj.getBMI();
     System.out.println("BMI = "+bmi);


}

答案 1 :(得分:0)

由于您是初学者,我会发布完整代码以帮助您前进。

public class BMI {
String name;
double height;
double weight;
public BMI(String name, double height, double weight){
   this.name=name;
   this.height=height;
   this.weight=weight;
}

public String getBMI() {
    return (weight/height);
  }


  public String toString() {
        return name + "is" + height + "tall and is " + weight +
                "and has a BMI of" + getBMI() ;
      }

public static void main(String[] args) {
     System.out.println(new BMI("Sample",2,4));

}

<强>输出

  

样品高2,为4,BMI为2

答案 2 :(得分:0)

你已经有了一个代码,所以我只提到BMI是体重(千克)除以身高(米)平方