2015-03-02 115 views
-1

此代碼的問題是計算每日所需的卡路里。無論男性還是女性,我都無法得到正確的數量。如果我可以改進任何事情,我都聽到了。謝謝。我是一名java初學者。卡路里計劃 - Java計算

public static void main(String[] args) 
    { 

     //variables 
     int age; 
     int height; 
     int weight; 
     String gender; 
     int BMR; 
     char genderChar; 
     boolean male; 
     String exercise; 
     boolean none = false; 
     boolean light = false; 
     boolean moderately = false; 
     boolean intensely = false; 
     boolean five; 
     double cal; 




     //Male or Female? 
     Scanner keyboard = new Scanner(System.in); 

     System.out.println("What is your gender? M or F: "); 
     gender = keyboard.nextLine(); 

     //Determining your BMR 

     System.out.println("What is your age: "); 
     age = keyboard.nextInt(); 

     System.out.println("What is your weight: "); 
     weight = keyboard.nextInt(); 

     System.out.println("What is your height: "); 
     height = keyboard.nextInt(); 

     genderChar = gender.charAt(0); 

     male = genderChar == 'M'; 

     if (male) 
     { 
      BMR = (int) (66 + (6.23 * weight) + (12.7 * height) - (6.8 * age)); 
     } 
     else 
     { 
      BMR = (int) (665 + (4.35 * weight) + (4.7 * height) - (4.7 * age)); 
     } 

     //Show BMR 

     if (male) 
     { 
      System.out.println("Your BMR is " + BMR); 
     } 
     else 
      System.out.println("Your BMR is " + BMR); 

     //Level of Exercises 

     if (none) 
     { 
      cal = (BMR * 1.2); 
     } 
     else if (light) 
     { 
      cal = (BMR * 1.375); 
     } 
     else if (moderately) 
     { 
      cal = (BMR * 1.55); 
     } 
     else if (intensely) 
     { 
      cal = (BMR * 1.725); 
     } 
     else 
     { 
      cal = (BMR * 1.9); 
     } 

     System.out.println("What is your level of exercise? "); 
     System.out.println("Type in none if you do not exercise. "); 
     System.out.println("Type in 2 if you engage in light exercise one to three days a week."); 
     System.out.println("Type in 3 if you do exercise moderately three to five times a week."); 
     System.out.println("Type in 4 if you do intensely six to seven days a week."); 
     System.out.println("Type in 5 if you do exercise intensely six to seven days a week and have a physically active job."); 

     exercise = keyboard.nextLine(); 
     none = keyboard.nextLine() != null; 

     System.out.println("Your daily calorie needs " + cal); 
    } 

} 

回答

1

幾件事情:

  • 您的代碼male = genderChar == 'M'; needstØ從掃描儀改爲male = genderChar.equalsIgnoreCase("M");由於nextLine將返回的字符串,而不是性格和比較字符串你需要的常量文字和匹配雙引號無論是m還是M(敏感匹配的情況下),您需要使用equalsIgnoreCase方法。
  • 你不需要以下,如果:

    if (male) 
    { 
        System.out.println("Your BMR is " + BMR); 
    } 
    else 
        System.out.println("Your BMR is " + BMR); 
    

只是說

System.out.println("Your BMR is " + BMR); 

- 您nonelight等varaibles永遠不會改變你初始化他們假後。所以可能你想看看他們根據你的要求適當設置。

+0

第一點:'genderChar = gender.charAt(0);' – jhamon 2015-03-02 13:27:35

+0

認爲,可能是另一種方式@jhamon – SMA 2015-03-02 13:48:29

+0

我的意思是他寫的東西。 – jhamon 2015-03-02 14:25:49

1

在您從 用戶獲得輸入之前,您正在計算cal的值。這樣,所有的布爾值將被設置爲「false」,這意味着您的 代碼到達else塊,並且只計算cal = (BMR * 1.9);。另外,我認爲你應該在這個例子中使用switch-case。

System.out.println("What is your level of exercise? "); 
    System.out.println("Type in none if you do not exercise. "); 
    System.out.println("Type in 2 if you engage in light exercise one to three days a week."); 
    System.out.println("Type in 3 if you do exercise moderately three to five times a week."); 
    System.out.println("Type in 4 if you do intensely six to seven days a week."); 
    System.out.println("Type in 5 if you do exercise intensely six to seven days a week and have a physically active job."); 

    exercise = keyboard.nextLine(); 

    //Use a switch-case now 
    switch(exercise) { 
    case "none": 
     //calculate cal here 
     break; //Don't forget the break!! 
    case "2": 
     //calculate cal here 
     break; //Don't forget the break!! 
    case "3": 
     //calculate cal here 
     break; //Don't forget the break!! 
    //continue with the rest here 
    } 

僅供參考,使用this tutorial