2012-12-15 295 views
0

程序基本上要求用戶輸入他們的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的程序,然後要求選擇一個活動級別並根據用戶是男性還是女性以及他們選擇?活動水平謝謝

回答

0

你只需要ActivityFactor設置一些虛擬值,以確保它的初始化:

double ActivityFactor = -1; 

希望幫助!

0

Java編譯器識別出您正在使用可能未初始化的變量:

double TD(ActivityFactor * BMR); 

這是因爲你的代碼路徑,允許這個變量保持未初始化:

else 
{ 

    System.out.println("Please enter a valid choice!"); 
} 

所以,有兩種選擇:初始化你聲明的變量:

double ActivityFactor = 0.0; 

或者製作所有的代碼路徑,以便這個變量在使用前必須被初始化。在你的情況下,它意味着你需要初始化添加到以下else塊:

else 
{ 
    ActivityFactor = 0.0; 
    System.out.println("Please enter a valid choice!"); 
} 

我建議你第一個選項中去。

0

您必須在if-else語句之前提供一個值ActivityFactor。如果你的任何'如果不操作,java不能知道ActivityFactor在

double TD(ActivityFactor * BMR); 

聲明。

申報和initilaze這樣的:

double ActivityFactor = 0.0; 
0

ActivityFactor在使用前進行初始化。您的代碼不能保證。最簡單的方法是在聲明中添加默認值:

double ActivityFactor = 10.0;