BMI 计算器分配.c#.我做错了什么

本文关键字:错了 什么 计算器 分配 BMI | 更新日期: 2023-09-27 18:32:23

嗨,我

刚开始上大学,我在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");
            }

            }

        }
    }
}

BMI 计算器分配.c#.我做错了什么

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.TryParse()