2016-01-31 39 views
-2

我是編碼的初學者。我正在創建一個BMI計算器,我應該使用2個參數:以千克爲單位的重量和以米爲單位的高度。使用Math.pow來平方高度。並返回一個雙值來計算BMI。用於Java的BMI計算器的參數

這是我到目前爲止。我知道這一切都搞砸了。幫助和建議表示讚賞。

{ 
    public static void displayWeight (double pounds)//pounds to kilograms 
    { 
     double weight = (pounds * 2.2);   
    } 
    public static void displayHeight (double inches)//inches to meters 
    { 
     double height = inches * 0.0264; 
    } 

public static void main (String[] args) 
{ 
    //greeting 
    System.out.print("I will calculate your BMI and how many calories you " 
      + "need to burn per day to maintain a healthy weight.\n\n"); 

    System.out.println("Please enter your weight in pounds: "); 
    Scanner keyboard = new Scanner(System.in); 
    double weight =keyboard.nextDouble(); 


    System.out.println("Please enter your height in inches: "); 
    double height =keyboard.nextDouble(); 

    System.out.println("Please enter your age in years: "); 
    double age =keyboard.nextDouble(); 

    double bmi = weight/Math.pow(height, 2); 

    System.out.print("Your Body Mass Index is:" + bmi); 

} 
}; 
+0

如果輸入的公斤重量和高度以米爲單位,您會得到正確的結果。 – Henry

+2

*「我知道它全部搞砸了」*>擴展。你的代碼有什麼問題?只是說它不起作用是不夠的。 –

回答

1

您要求用戶輸入磅和英寸,但是您要對BMI進行米和千克計算。

爲計算公斤磅是

double kilograms = pounds /2.2; 

所以在這裏你行應該是:

double weight = keyboard.nextDouble()/2.2; 

和你英寸高度

double meters = inches * 0.0254; 

所以你行應:

double height =keyboard.nextDouble() * 0.0254; 

您現在應該擁有正確的BMI。

+0

我添加了這些行。它說我的變量沒有被使用。 – Luke8h

+1

@ Luke8h你真的應該學習一些基礎知識,而不是在這裏發佈你的作業。 – user3437460