程序基本上要求用戶輸入他們的BMR和性別。然後他們被要求選擇一個活動水平(如果他們選擇錯誤的,我需要打印「請輸入一個有效的選擇」)。女性和男性有不同的活動因素。我遇到「變量ActivityFactor必須初始化」的錯誤。我如何修復我的代碼?謝謝初始化變量
iBMR = new Scanner(System.in);
Scanner iGender = new Scanner(System.in);
Scanner iActivity = new Scanner(System.in);
String AcFactor;
double ActivityFactor ;
double TDEE;
//User Input
System.out.println("Please enter your name: ");
String inName = iName.next();
System.out.println("Please enter your BMR: ");
String inBMR = iBMR.next();
double nBMR = Double.parseDouble(inBMR);
System.out.println("Please enter your Gender");
String inGender = iGender.next();
System.out.println("Select Your Activity Level");
System.out.println("[A] Resting (Sleeping,Reclining)");
System.out.println("[B] Sedentary (Minimal Movement)");
System.out.println("[C] Light (Sitting, Standing)");
System.out.println("[D] Moderate (Light Manual Labor, Dancing, Riding Bike)");
System.out.println("[E] Very Active (Team Sports, Hard Manual Labor)");
System.out.println("[F] Extremely Active (Full-time Athlete, Heavy Manual Labor)");
System.out.println("Enter the letter corrresponding to your activity level: ");
String inActivity = iActivity.next();
//CalculationFemale
if(inActivity.equalsIgnoreCase("A")&& inGender.equalsIgnoreCase("F"))
{
ActivityFactor = 1.0;
}
else if(inActivity.equalsIgnoreCase("B")&& inGender.equalsIgnoreCase("F"))
{
ActivityFactor = 1.3;
}
else if(inActivity.equalsIgnoreCase("C")&& inGender.equalsIgnoreCase("F"))
{
ActivityFactor = 1.5;
}
else if(inActivity.equalsIgnoreCase("D")) && (inGender.equalsIgnoreCase("F"))
{
ActivityFactor = 1.6;
}
else if(inActivity.equalsIgnoreCase("E")&& inGender.equalsIgnoreCase("F"))
{
ActivityFactor = 1.9;
}
else if(inActivity.equalsIgnoreCase("F")&& inGender.equalsIgnoreCase("F"))
{
ActivityFactor = 2.2;
}
else
{
System.out.println("Please enter a valid choice!");
}
//CalculationMale
if(inActivity.equalsIgnoreCase("A")&& inGender.equalsIgnoreCase("M"))
{
ActivityFactor = 1.0;
}
else if(inActivity.equalsIgnoreCase("B")&& inGender.equalsIgnoreCase("M"))
{
ActivityFactor = 1.3;
}
else if(inActivity.equalsIgnoreCase("C")&& inGender.equalsIgnoreCase("M"))
{
ActivityFactor = 1.6;
}
else if(inActivity.equalsIgnoreCase("D")&& inGender.equalsIgnoreCase("M"))
{
ActivityFactor = 1.7;
}
else if(inActivity.equalsIgnoreCase("E")&& inGender.equalsIgnoreCase("M"))
{
ActivityFactor = 2.1;
}
else if(inActivity.equalsIgnoreCase("F")&& inGender.equalsIgnoreCase("M"))
{
ActivityFactor = 2.4;
}
else
{
System.out.println("Please enter a valid choice!");
}
//TDEE
double TD(ActivityFactor * BMR);
**謝謝你的所有答案。我意識到如果所有if/if條件都不通過,那麼「else」對於ActivityFactor將沒有值。感謝所有的答覆,我現在意識到我的追求這種方式是行不通的。有人可以給我一個綱要,告訴我如何編寫一個要求用戶名/性別/ bmr的程序,然後要求選擇一個活動級別並根據用戶是男性還是女性以及他們選擇?活動水平謝謝