2016-01-31 18 views
1

我是Java的新手,試圖製作一個基本的體重計算器。 我的問題是我需要問的問題,轉換測量,然後將其傳遞給另一種方法,然後以單獨的方法顯示結果。 我已經在下面添加了我的代碼,但每次都收到1.0返回的答案。基本的Java,在方法之間傳遞數據

import java.util.Scanner; 

public class calcBMI { 

    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 

     System.out.print("Enter weight in pounds: "); 
     double weightInPounds = keyboard.nextDouble(); 
     double weightInKg = (weightInPounds/2.2); 

     System.out.print("Enter height in inches: "); 
     double heightInInches = keyboard.nextDouble(); 
     double heightInMeters = (heightInInches/0.254); 
     double resultBMI = 1; 

     displayResults(resultBMI); 
    } 

    public static double bodyMassIndex(double weightInKg, double 
    heightInMeters) 
    { 
     double resultBMI = weightInKg/Math.pow(heightInMeters, 2) * 1.375; 

     return resultBMI; 
    }   

    public static void displayResults(double resultBMI) 
    { 

     System.out.printf("The calculated body mass index was: " + resultBMI); 

     System.out.println(); 
    } 
} 

更新代碼,現在越來越; 磅輸入重量:180 以英寸輸入高度:68 計算的身體質量指數是:1.1415618118905313E -5- 生成成功(總時間:3秒)

import java.util.Scanner; 

public class calcBMI { 

    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 

     System.out.print("Enter weight in pounds: "); 
     double weightInPounds = keyboard.nextDouble(); 
     double weightInKg = (weightInPounds/2.2); 

     System.out.print("Enter height in inches: "); 
     double heightInInches = keyboard.nextDouble(); 
     double heightInMeters = (heightInInches/0.0254); 


     displayResults(bodyMassIndex(weightInKg, heightInMeters)); 
    } 

public static double bodyMassIndex(double weightInKg, double heightInMeters) 
    { 
     return (weightInKg/Math.pow(heightInMeters, 2)); 
    }   

public static void displayResults(double resultBMI) 
    { 

     System.out.printf("The calculated body mass index was: " + resultBMI); 

     System.out.println(); 
    } 
} 
+0

我還需要使用printf的方法將BMI結果四捨五入到1位小數...我已經嘗試添加代碼System.out.printf(「計算出的體重指數爲: %.1f「+ resultBMI);但是我這樣做時出現錯誤 – Newb2Java

+0

首先更正您的高度計算。然後用'160磅'和'70英寸'測試重量和身高。你應該得到'BMI 23'。它會工作。 – user3437460

+0

聖潔的蝙蝠俠它的工作原理。有什麼想法,我可以如何四捨五入到一位小數? – Newb2Java

回答

0

你得到1.0因爲你硬編碼它。

更改此:

double resultBMI = 1; 

要:

double resultBMI = bodyMassIndex(weightInKG, heightInMeters); 

順便問一下,你也可以有直接的方法返回BMI並沒有無需通過乘以1.375因爲您已經提供KG的體重:

public static double bodyMassIndex(double weightInKg, double heightInMeters) 
{ 
    return (weightInKg/(heightInMeters*heightInMeters)); 
} 

添加上:

你從英寸轉換爲米是錯誤的爲好。它應該是:

double heightInMeters = (heightInInches * 0.0254); 
+0

我做了更改,但現在我得到的價值返回; 計算的體重指數是:0.0011415618118905313,我應該在10到30之間,因爲18.5到24.9是正常的 – Newb2Java

+0

@BradHillis那是因爲你的轉換是錯誤的。 1英寸= 0.0254米,不是0.254。 – user3437460

+0

現在正在進行修正,計算出的體重指數是:1.1415618118905313E-5 – Newb2Java

1

您還沒有調用bodyMassIndex方法在你的代碼中。更改

displayResults(resultBMI); 

displayResults(bodyMassIndex(weightInKg, heightInMeters)); 

resultBMI等於,所以當然的輸出將永遠是:

"The calculated body mass index was: 1" 

全碼:

public static void main(String[] args) { 

    System.out.print("Enter weight in pounds: "); 
    double weightInPounds = keyboard.nextDouble(); 
    double weightInKg = (weightInPounds/2.2); 

    System.out.print("Enter height in inches: "); 
    double heightInInches = keyboard.nextDouble(); 
    double heightInMeters = (heightInInches/0.254);   

    // You can get rid of the resultBMI variable 
    displayResults(bodyMassIndex(weightInKg, heightInMeters)); 
} 
+0

你可能想讓這些浮點文字雙精度文字......並防止隱式投射,就像這樣:'2.2d'和'0.254d' – RAnders00

+1

那些已經是雙文字。對於浮點數,double是Java中的默認值。 – SpiderPig

+0

我做了更改,但現在我得到一個返回的值; 「計算的體重指數是:0.0011415618118905313,」我應該在10到30之間,因爲18.5到24.9是正常的 – Newb2Java