2014-09-03 245 views
-4

我对java尝试使用构造函数,公共实例方法和toString方法构建简单的BMI计算器非常陌生。JAVA: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) { 


} 

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

谢谢:)

+0

如何借“像初学者的Java”或者试用一些介绍性的教程?我真的很困惑,在写这篇评论的时候,你已经得到了两个完整的答案来显示ZERO的努力。对这种调查的常见回答是“你试过了什么?”和“我们不提供完整的家庭作业解决方案”,但是很好。 – hiergiltdiestfu 2014-09-03 12:59:08

回答

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


} 
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,具有2

0

一个BMI你已经有一个代码,这样我就提到,BMI是体重(单位为千克)除以身高(单位为米)平方