【问题标题】:JAVA: BMI Calculator usingJAVA:使用 BMI 计算器
【发布时间】:2014-10-27 23:35:49
【问题描述】:

我对 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) {


}

我真的不知道自己在做什么,因此感谢所有帮助。如果您知道如何完成此操作并可以向我展示一个可以演示如何使用它的主要方法,那将更加感激。

谢谢:)

【问题讨论】:

  • 借一本《Java 初学者》之类的书,或者尝试一些介绍性教程怎么样?老实说,我很困惑,在写这篇评论的时候,你已经得到了两个完整的答案来展示零努力。对这种询问的通常回答是“你试过什么?”和“我们不提供完整的家庭作业解决方案。”,但是很好。

标签: java arraylist double


【解决方案1】:

您已经有一个代码,所以我只提一下 BMI 是体重(以公斤为单位)除以身高(以米为单位)平方

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      由于您是初学者,因此我发布了完整的代码以帮助您入门。

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-04
        • 2018-03-03
        • 2016-11-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多