计算剩余卡路里输出

时间:2017-10-29 00:50:04

标签: c# loops monodevelop

我已经完成了大部分代码,但最后一部分现在已经混乱了大约2个小时。我试图通过减去他们吃的食物/零食的卡路里所允许的卡路里来向用户显示剩余的卡路里,他们说是“是”以继续。我每次都会收到错误。非常感谢任何帮助!

using System;

namespace CSE1101Unit2Lab
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string name;
            double height, weight;
            int userAge;
            string gender;
            double exerciseFactor;
            double BMR = 0;
            double DailyCI = 0;

            Console.Write ("Enter your name: ");
            name = Console.ReadLine ();
            Console.Write ("Enter your height in inches: ");
            height = Convert.ToDouble (Console.ReadLine ());
            Console.Write ("Enter your weight in pounds: ");
            weight = Convert.ToDouble (Console.ReadLine ());
            Console.Write ("Enter your age: ");
            userAge = Convert.ToInt32 (Console.ReadLine ());
            Console.Write ("Enter your gender as M or F: ");
            gender = Console.ReadLine ();
            gender = gender.ToUpper ();
            Console.Write ("Please select your level of activity: ");

            Console.WriteLine ("1. You don't exercise");
            Console.WriteLine ("2. You engage in light exercise one to three days a week");
            Console.WriteLine ("3. You exercise moderately three to five times a week"); 
            Console.WriteLine ("4. You exercise intensely six to seven days a week"); 
            Console.WriteLine ("5. you exercise intensely six to seven days a week and have a physically active job"); 
            Console.WriteLine ("Activity Level: ");

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

            if (gender == "F") {
                BMR = 655 + (4.35 * weight) + (4.7 * height) - (4.7 * userAge);

                if (exerciseFactor == 1) {
                    DailyCI = BMR * 1.2;
                }
                if (exerciseFactor == 2) {
                    DailyCI = BMR * 1.375;
                }
                if (exerciseFactor == 3) {
                    DailyCI = BMR * 1.55;
                }
                if (exerciseFactor == 4) {
                    DailyCI = BMR * 1.725;
                }
                if (exerciseFactor == 5) {
                    DailyCI = BMR * 1.9;

                }
            } else if (gender == "M") {
                BMR = 66 + (6.23 * weight) + (12.7 * height) - (6.8 * userAge);

                if (exerciseFactor == 1) {
                    DailyCI = BMR * 1.2;
                }
                if (exerciseFactor == 2) {
                    DailyCI = BMR * 1.375;

                }
                if (exerciseFactor == 3) {
                    DailyCI = BMR * 1.55;

                }
                if (exerciseFactor == 4) {
                    DailyCI = BMR * 1.725;

                }
                if (exerciseFactor == 5) {
                    DailyCI = BMR * 1.9;


                }
                Console.WriteLine (name + " you entered: \nHeight: " + height + "\nWeight: " + weight + "\nAge: " + userAge + "\nGender: " + gender);
                Console.WriteLine ("Your BMR is " + BMR);
                Console.WriteLine ("Your daily calories allowed is " + DailyCI);


                {

                }
                double caloriesAllowed = BMR * exerciseFactor;
                string response = "YES";
                while (response == "YES");
                var calconsumed = 0;
                {
                    Console.WriteLine ("Enter the amount of calories consumed: ");
                    calconsumed = Convert.ToInt32 (Console.ReadLine ());
                    Console.WriteLine ("Your remaining calories are:" + caloriesAllowed - calconsumed);
                    Console.WriteLine("Do you want to continue? (YES / NO)");
                    response = Console.ReadLine().ToUpper();
                        response = response.ToUpper();
                    }
                }
        }
    }
}

如果用户键入YES继续,则现在不循环。如何让这段代码循环?

            {
            double caloriesAllowed = BMR * exerciseFactor;
            var calconsumed = 0;
            string response = "YES";
                do {

                    Console.WriteLine ("Enter the amount of calories consumed: ");
                    Console.WriteLine ("Your remaining calories are:" + (caloriesAllowed - calconsumed));
                    calconsumed = Convert.ToInt32 (Console.ReadLine();
                    Console.WriteLine ("Do you want to continue? YES or NO: ");
                    Console.ReadLine ();
                    response = Console.ReadLine().ToUpper();

                } while (response == "YES");

1 个答案:

答案 0 :(得分:0)

你构建一个空的无限循环,因为你在while条件之后使用分号而不是大括号。

这应该有效

        string response = "YES";
          var calconsumed = 0;
        while (response == "YES")
        {

            Console.WriteLine("Enter the amount of calories consumed: ");
            calconsumed = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Your remaining calories are:" + (caloriesAllowed - calconsumed));
            Console.WriteLine("Do you want to continue? (YES / NO)");
            response = Console.ReadLine().ToUpper();
            response = response.ToUpper();
        }