2013-12-12 216 views
-1
import java.util.Scanner; 

public class BMR 

{ 
public static void main(String[] args) 

{ 
    //object to use the keyboard input methods 

    Scanner in = new Scanner(System.in); 



    //ask the user to provide input 

    System.out.print("Enter your name: "); 

    String name = in.next(); 

    System.out.print("Gender (M of F): "); 

    String gender = in.next(); 

    System.out.print("Enter your age: "); 

    String age = in.next(); 

    System.out.print("Height in inches: "); 

    String height = in.next(); 

    System.out.println("Weight in pounds: "); 

    String weight = in.next(); 

    System.out.println(); 

    System.out.println(); 




    //convert measurements to metric units 

    double ageNum = Double.parseDouble(age); 

    double heightNum = Double.parseDouble(height); 

    double weightNum = Double.parseDouble(weight); 

    double heightCm = heightNum * 2.54; 

    double weightKilo = weightNum * 0.453592; 



    //calculate the BMR for males or females 

    double bmr; 

    if (gender == "M") 
     bmr = (13.397 * weightKilo) + (4.799 * heightCm) - (5.677 * ageNum) + 88.362; 

    else if (gender == "F") 
     bmr = (9.247 * weightKilo) + (3.098 * heightCm) - (4.330 * ageNum) + 447.593; 

    else 
     System.out.println("Please enter either M or F. I apologize for the gender binary."); 

    //display output 

    System.out.println("Calculate Your Basal Metabolism"); 

    System.out.println(); 

    System.out.println("Name: " + name); 

    System.out.println("Gender: " + gender); 

    System.out.println("Age: " + age); 

    System.out.println("Weight (kg): " + weightKilo); 

    System.out.println("Height (cm): " + heightCm); 

    System.out.println("Basal Metabolic Rate: " + bmr + " calories per day"); 





} 
} 

我必須爲我的在線課程編寫此代碼來計算某人的基礎代謝率。每次運行它時,我都會收到一個錯誤消息,說變量bmr可能沒有被初始化。但我在條件語句中初始化了它。我究竟做錯了什麼?謝謝!變量bmr可能尚未初始化

+0

您需要初始化bmr,如錯誤消息所示,您只聲明瞭it.Inialialize爲double bmr = 0.0; – Suman

+0

搜索錯誤:[\ [java \]變量可能尚未初始化](http://stackoverflow.com/search?q=%5Bjava%5D+variable+might+not+have+been+initialized)和你會發現*很多*重複。其實,甚至不搜索。只要看看相關的問題 - 不妨現在就去做,因爲在提出問題時顯然忽略了這些建議。 – user2864740

回答

1

double bmr;變化double bmr=0;

你沒有初始化局部變量。

你的代碼還有一個錯誤。 genderString,您正在比較String使用==這是錯誤的。你應該使用的

if (gender == "M") 
+0

當我把'雙bmr = 0',然後執行程序它根本不計算bmr,它只是保持0. – user3094477

+1

@ user3094477看到我的答案的其餘部分。你的'if'條件不滿足,因爲你的代碼在'=='中用'String'發佈。使用'if(「M」.equals(gender))'; –

1

equals()

使用

if("M".equals(gender)) 

相反,你可以使用:

double bmr = 0.0; 

順便說一句,你也應該使用:

"M".equals(gender) 

代替

gender == "M" 

這個錯誤發生,因爲如果性別不是"M""F"bmr不會當您使用它初始化。

0

Local variables and primitives should be initialized before use because you would know what to expect from the values. Historically, when a new variable was created it would contain random values from the memory [and one could not predict the value]. Java also requires this because it prevents the presence of orphaned variables.

變化

double bmr; 

double bmr=0; 

還可以使用

if("M".equals(gender)) 

,而不是

if (gender == "M") 
0

按Java的語法規則,方法的局部變量應該初始化。您尚未初始化您的本地變量。所以改變:

double bmr ; 

double bmr = 0.0; 
0

當你不初始化所有局部變量,會出現此錯誤。

你的情況,你必須做出如下修改:

double bmr = 0; 
0

你要的值設置爲變量之前在所有情況下使用它

在最後一個else中,bmr未初始化,但如果未輸入性別,則可能不希望顯示輸出,因此,您可以在else中添加return子句以避免它。

else { 
     System.out.println("Please enter either M or F. I apologize for the gender binary."); 
     return; 
    } 

如果你想也顯示輸出在這種情況下,你必須提供一個valure到BMT:

else { 
     System.out.println("Please enter either M or F. I apologize for the gender binary."); 
     bmr = 0; 
    } 
0
double bmr; //initialization is not necessary here if we use following way around 

    if (gender == "M") 
    { 
     bmr = (13.397 * weightKilo) + (4.799 * heightCm) - (5.677 * ageNum) + 88.362; 
     System.out.println(bmr); //we are using bmr but after initialization in above line. So compiler doesn't complain. 
    } 

上面的代碼是很確定,當您在使用左側bmr=符號的一側,您可以使用初始化後打印其值。在使用它之前,必須初始化變量。

但在以下情況下bmr在使用之前尚未初始化。

double bmr; 

    if (gender == "M") 
    { 
     System.out.println(bmr); //compiler complains here 
    } 

改正它初始化bmr爲某個值,

double bmr=111; 

     if (gender == "M") 
     { 
      System.out.println(bmr); 
     } 

現在讓我們從您的代碼段檢查不正確的代碼。

double bmr; 
System.out.println("Basal Metabolic Rate: " + bmr + " calories per day"); //you have written this line next after 13 to 14 lines of code after `bmr` declaration where compiler complains for initialization;