2014-09-03 246 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是體重(單位爲千克)除以身高(單位爲米)平方