BMI计算器分配。 C#。我究竟做错了什么?

时间:2014-11-29 12:54:56

标签: c#

您好,我刚开始上大学,我在c#有一项任务,我必须创建一个可以计算公制和英制BMI的BMI计算器。我创建了下面的代码,其中提出了许多错误,但我不知道为什么。有什么想法吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BMI_CALCULATOR
{
    class Program
    {
        static void Main(string[] args)
        {
            int weightPounds, WeightKilo, heightMeters, heightInches, Menu;
           double BMI;



            Console.WriteLine("1. Imperial");
            Console.WriteLine("2. Metric");
            Console.WriteLine("3. Exit);
            Console.WriteLine("press 1 for Imperial, 2 for metric or 3 to exit: ");
           Menu = Convert.ToInt32(Console.ReadLine());

            if (Menu == 1)
            {
            Console.Write("please enter your weight in pounds: ");
            weightPounds = Convert.ToInt32(Console.ReadLine());


            Console.Write("please enter your height in inches: ");
            heightInches = Convert.ToInt32(Console.ReadLine());

            BMI = weightPounds / Math.Pow (heightInches, 2);

                if (BMI < 18.5) 
                {
            Console.WriteLine("your BMI is {0:C}" , BMI, "you are underweight");
                }

             else if ((BMI >= 18.5) && (<= 24.9)) 
                { 
                    Console.WriteLine("your BMI is {0:C}" , BMI, "you are normal");
                }

                else if (BMI > 24.9 )
                {
                    Console.WriteLine("your BMI is {0:C}" , BMI, "you are overweight");
                }

            }


            else if (Menu == 2)
            {
                  Console.Write("please enter your weight in pounds: ");
            weightPounds = Convert.ToInt32(Console.ReadLine());


            Console.Write("please enter your height in inches: ");
            heightInches = Convert.ToInt32(Console.ReadLine());

            BMI = WeightKilo / Math.Pow (heightMeters, 2);

             if (BMI < 18.5)
            {

             {
                  Console.WriteLine("your BMI is {0:C}" , BMI, "you are underweight");
             }

                  else if ((BMI >= 18.5) && (<= 24.9)) 
                { 
                    Console.WriteLine("your BMI is {0:C}" , BMI, "you are normal");
                }


                else if (BMI > 24.9 )
                {
                    Console.WriteLine("your BMI is {0:C}" , BMI, "you are overweight");
                }
            }


            else if (Menu == 3)
            {
                Console.WriteLine ("end program");

            }



            }


        }
    }
}

1 个答案:

答案 0 :(得分:1)

else if (Menu == 2)
{
    Console.Write("please enter your weight in pounds: ");
    weightPounds = Convert.ToInt32(Console.ReadLine());
    Console.Write("please enter your height in inches: ");
    heightInches = Convert.ToInt32(Console.ReadLine());

    BMI = WeightKilo / Math.Pow (heightMeters, 2);
    //your code
}

在这里,您将度量值读入英制变量,然后使用公制变量(已经未初始化)。

如果要从字符串解析double,请使用Double.TryParse()