while循环从变量

时间:2017-03-06 03:58:58

标签: c# loops

好的,所以我有一个程序要求年龄,体重,身高和性别。然后它使用if语句相应地计算你的BMR,然后它使用更多的if语句来计算你的每日卡路里摄入量。 (DCI)在显示DCI之后,我需要一个WHILE循环,它要求用户输入卡路里量,然后从DCI中减去它,使其变为剩余的卡路里。我需要帮助的代码位于最底层。有人告诉我,我不需要为用户输入的数字做一个变量,但可以直接从DCI中减去它,这样就可以变成剩余的卡路里。

    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)");
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

DCI变量属于double类型,因此我建议您使用double.TryParse代替Convert.ToInt32。然后你必须从用户那里读取yes或no选项,所以修改while,如下所示:

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");
   }
}

答案 1 :(得分: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();
    }

答案 2 :(得分: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计算似乎有误。此外,此代码的错误检查非常少,但只要您输入有效的输入,它就可以正常运行。