2013-12-12 167 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;