2017-03-06 30 views
-2

好吧,所以我有一個程序要求年齡,體重,身高和性別。然後它使用if語句來相應地計算您的BMR,然後使用更多if語句來計算您的每日卡路里攝入量。 (DCI)在顯示DCI後,我需要一個WHILE循環,它要求用戶輸入卡路里量,並從DCI中減去它,以便它變成卡路里剩餘量。我需要幫助的代碼處於最底層。有人告訴我,我不需要爲用戶輸入的號碼創建一個變量,但可以直接從DCI中減去它,從而變成卡路里剩餘。while循環從變量中減去用戶輸入

using System; 

namespace Shaft_Lab4 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 

      int weight, height, age, gender; 

      double exerciseFactor; 
      double DCI = 0; 



      Console.Write("Enter your age in years "); 
      age = Convert.ToInt32 (Console.ReadLine()); 
      Console.WriteLine ("Enter your weight in pounds "); 
      weight = Convert.ToInt32 (Console.ReadLine()); 
      Console.WriteLine ("Enter your height in inches "); 
      height = Convert.ToInt32 (Console.ReadLine()); 

      Console.WriteLine ("Gender? Enter male/female (1 for Male, 2 for Female) "); 
      gender= Convert.ToInt32 (Console.ReadLine()); 

      Console.WriteLine("To calculate your daily calories allowed, please select your level of exercise activity"); 

      // exercise intensity levels 

      Console.WriteLine("1. You don't exercise"); // bmr x 1.2 
      Console.WriteLine("2. You engage in light exercise one to three days a week"); // bmr x 1.375 
      Console.WriteLine("3. You exercise moderately three to five times a week"); // bmr x 1.55 
      Console.WriteLine("4. You exercise intensely six to seven days a week"); // bmr x 1.725 
      Console.WriteLine("5. you exercise intensely six to seven days a week and have a physically active job"); // bmr x 1.9  

      exerciseFactor = Convert.ToDouble (Console.ReadLine()); 

      // MALE if statements 

      if (gender == 1) 
      { 
       Console.WriteLine ("Age: " + age); 
       Console.WriteLine ("Height: " + height); 
       Console.WriteLine ("Weight: " + weight); 
       Console.WriteLine ("Gender: Male"); 

       double maleBMR = (66 + (6.23 * weight) + (12.7 * height) - (6.8* age)); 


       Console.WriteLine ("Your BMR is: " + maleBMR); 

       if (exerciseFactor == 1) { 
        DCI = maleBMR * 1.2; 
        Console.WriteLine ("Your daily calories allowed is " + DCI); 
       } 
       if (exerciseFactor == 2) { 
        DCI = maleBMR * 1.375; 
        Console.WriteLine ("Your daily calories allowed is " + DCI); 

       } 
       if (exerciseFactor == 3) { 
        DCI = maleBMR * 1.55; 
        Console.WriteLine ("Your daily calories allowed is " + DCI); 

       } 
       if (exerciseFactor == 4) { 
        DCI = maleBMR * 1.725; 
        Console.WriteLine ("Your daily calories allowed is " + DCI); 

       } 
       if (exerciseFactor == 5) { 
        DCI = maleBMR * 1.9; 
        Console.WriteLine ("Your daily calories allowed is " + DCI); 
       } 




    // FEMALE if statements 

      } 


      if (gender == 2) 
      { 
       Console.WriteLine ("Age: " + age); 
       Console.WriteLine ("Height: " + height); 
       Console.WriteLine ("Weight: " + weight); 
       Console.WriteLine ("Gender: Female"); 

       double femaleBMR = (655 + (4.35 * weight) + (4.7 * height) - (4.7 * age)); 

       Console.WriteLine ("Your BMR is: " + femaleBMR); 

       if (exerciseFactor == 1) { 
        DCI = femaleBMR * 1.2; 
        Console.WriteLine ("Your daily calories allowed is " + DCI); 
       } 
       if (exerciseFactor == 2) { 
        DCI = femaleBMR * 1.375; 
        Console.WriteLine ("Your daily calories allowed is " + DCI); 
       } 
       if (exerciseFactor == 3) { 
        DCI = femaleBMR * 1.55; 
        Console.WriteLine ("Your daily calories allowed is " + DCI); 
       } 
       if (exerciseFactor == 4) { 
        DCI = femaleBMR * 1.725; 
        Console.WriteLine ("Your daily calories allowed is " + DCI); 
       } 
       if (exerciseFactor == 5) { 
        DCI = femaleBMR * 1.9; 
        Console.WriteLine ("Your daily calories allowed is " + DCI); 
       } 



      } 


      //THIS IS WHERE I NEED HELP 

      string response = "YES"; 

      while (response == "YES") { 
       Console.WriteLine ("Enter the amount of calories consumed: "); 
       Convert.ToInt32 (Console.ReadLine()); 

       Console.WriteLine ("Do you want to continue? (YES/NO)"); 
      } 
     } 
    } 
} 

回答

0

您所需要的主要部分是這樣的:

string response = "YES"; 
    while (response == "YES") 
    { 
     caloriesAllowed -= ConsoleReadInteger("Enter the amount of calories consumed: "); 
     Console.WriteLine("Your remaining calories allowed is " + caloriesAllowed); 
     Console.WriteLine("Do you want to continue? (YES/NO)"); 
     response = Console.ReadLine().ToUpperInvariant(); 
    } 

這裏是整個代碼 - 我已經重構了你,讓你可以看到如何與你的一些編碼更高效:

public static int ConsoleReadInteger(string message) 
{ 
    Console.WriteLine(message); 
    return Convert.ToInt32(Console.ReadLine()); 
} 

public static void Main(string[] args) 
{ 
    int age = ConsoleReadInteger("Enter your age in years "); 
    int weight = ConsoleReadInteger("Enter your weight in pounds "); 
    int height = ConsoleReadInteger("Enter your height in inches "); 
    int gender = ConsoleReadInteger("Gender? Enter male/female (1 for Male, 2 for Female) "); 

    Console.WriteLine("To calculate your daily calories allowed, please select your level of exercise activity"); 

    Console.WriteLine("1. You don't exercise"); // bmr x 1.2 
    Console.WriteLine("2. You engage in light exercise one to three days a week"); // bmr x 1.375 
    Console.WriteLine("3. You exercise moderately three to five times a week"); // bmr x 1.55 
    Console.WriteLine("4. You exercise intensely six to seven days a week"); // bmr x 1.725 
    Console.WriteLine("5. you exercise intensely six to seven days a week and have a physically active job"); // bmr x 1.9  

    Dictionary<int, double> exerciseFactors = new Dictionary<int, double>() 
    { 
     { 1, 1.2 }, { 2, 1.375 }, { 3, 1.55 }, { 4, 1.725 }, { 5, 1.9 }, 
    }; 

    double exerciseFactor = exerciseFactors[Convert.ToInt32(Console.ReadLine())]; 

    Console.WriteLine("Age: " + age); 
    Console.WriteLine("Height: " + height); 
    Console.WriteLine("Weight: " + weight); 
    Console.WriteLine("Gender: " + (gender == 1 ? "Male" : "Female")); 

    double bmr = 
     gender == 1 
     ? 66 + (6.23 * weight) + (12.7 * height) - (6.8 * age) 
     : 655 + (4.35 * weight) + (4.7 * height) - (4.7 * age); 

    double caloriesAllowed = bmr * exerciseFactor; 

    Console.WriteLine("Your BMR is: " + bmr); 
    Console.WriteLine("Your daily calories allowed is " + caloriesAllowed); 

    string response = "YES"; 
    while (response == "YES") 
    { 
     caloriesAllowed -= ConsoleReadInteger("Enter the amount of calories consumed: "); 
     Console.WriteLine("Your remaining calories allowed is " + caloriesAllowed); 
     Console.WriteLine("Do you want to continue? (YES/NO)"); 
     response = Console.ReadLine().ToUpperInvariant(); 
    } 
} 

在計算BMR時似乎存在錯誤。此外,這段代碼的錯誤檢查很少,但只要你輸入有效的輸入,它應該運行良好。

+0

非常有幫助,但它不能識別控制檯讀取整數? –

+0

@JasonShaft - 'ConsoleReadInteger'在我的答案中的代碼中。 – Enigmativity

+0

是的,它不起作用。無論如何,我得到的程序工作,我需要做的只是切換兩個簡單的詞 –

0

DCI變量是double類型的,所以我想建議你使用double.TryParse而不是爲Convert.ToInt32。那麼你必須閱讀用戶的yes或no選項,所以修改一下,如下所示:

while (response == "YES") 
{ 
    double userInput; 
    Console.WriteLine ("Enter the amount of calories consumed: "); 
    if(double.TryParse(Console.ReadLine(), out userInput); 
    { 
     DCI -= value; // performing subtraction 
     Console.WriteLine("Do you want to continue? (YES/NO)"); 
     response = Console.ReadLine().ToUpper(); 
    } 
    else 
    { 
     Console.WriteLine("Invalid input"); 
    } 
} 
+0

更新了,現在我只是混淆瞭如何讓用戶輸入從我的DCI變量中減去.. –

+0

@JasonShaft:我已經更新了答案請看看 –

0

只是循環,讀入並減去它。

string response = "YES"; 
var value = 0; 

    while (response == "YES") { 
     Console.WriteLine ("Enter the amount of calories consumed: "); 
     Int.TryParse(Console.ReadLine(), out value); 

     DCI -= value; 

     Console.WriteLine("Do you want to continue? (YES/NO)"); 
     response = Console.ReadLine().ToUpper(); 
    }