2017-03-06 11 views
-2

これで、年齢、体重、身長、性別を問うプログラムがあります。それに応じて、if文を使用してBMRを計算し、if文を使用してDaily Calorie Intakeを計算します。 (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

ええ、それは動作しません。いいえ、私はプログラムを稼働させました、私がする必要があったのは2つの簡単な言葉を切り替えることでした。 –

0

DCI変数はdouble型であるので、私はあなたがConvert.ToInt32のための代わりにdouble.TryParseを使用することをお勧めしたいと思います。

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

だけループ、それを読んで、それを引く:あなたは、ユーザからのyesまたはnoオプションを読んで、その次のようにしばらくを変更する必要があります。

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(); 
    } 
関連する問題