CS0165:使用未分配的局部变量`gender'

时间:2017-09-10 02:38:55

标签: c#

我正在尝试创建一个脚本,用于计算男性和女性的BMR并显示结果。但是我遇到了一个错误,显示错误CS0165:使用未分配的局部变量`gender'。

using System;

namespace CSE1101Unit2Lab
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Console.Write ("Hello, what is your name?: ");
            string userName = Console.ReadLine ();
            Console.Write (" What is your age?: ") ;
            int age = int.Parse (Console.ReadLine ());
            Console.Write (" What is your weight in pounds?: ");
            int weight = int.Parse (Console.ReadLine ());
            Console.Write (" What is your height in inches?: ");
            int height = int.Parse (Console.ReadLine ());
            Console.Write (" What is your gender? Male or Female? (please type M = male and F = female): ");
            string gender;
            double genderFemale = double.Parse (Console.ReadLine ());
            double genderMale = double.Parse (Console.ReadLine ());




            Console.WriteLine (" Your age: " + age);
            Console.WriteLine (" Your weight: " + weight);
            Console.WriteLine (" Your height: " + height);
            Console.WriteLine (" Your gender: " + gender);


            if (gender == "F") 

            {
                genderFemale = 655 + (4.35 * weight) + (4.7 * height) - (4.7 * age);
            }
            else 

            {
                genderMale = 66 + (6.23 * weight) + (12.7 * height) - (6.8 * age);
            }
            int bmr = int.Parse (Console.ReadLine ());
            Console.WriteLine (" Your BMR is: " + bmr);





        }
    }
}

1 个答案:

答案 0 :(得分:0)

您声明gender但不指定默认值,并且您从未设置它。

我认为你应该改变这个

string gender;

到这个

string gender = Console.ReadLine();