不能隐式地将类型int转换为bool,使用bool和运算符 - 不能应用

时间:2017-01-25 00:20:29

标签: c# logic syntax-error

我在尝试制作一个要求用户年龄,体重,身高和性别的BMR计算器时遇到错误。我正在使用if语句来确定要打印的BMR的公式。但我一直收到错误:

  

无法将int类型隐式转换为bool

我是初学者,之前从未见过他们。这是我的代码。

public static void Main (string[] args)
{
    int weight, height, age, gender;

    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());

    if (gender = 1)
    {
        Console.WriteLine ("Age: " + age);
        Console.WriteLine ("Height:" + height);
        Console.WriteLine ("Weight:" + weight);
        Console.WriteLine ("Gender:" + gender);

        Console.WriteLine ("Your BMR is" + 66 + (6.23 * weight) + (12.7 * height) - (6.8* age));
    }

    if (gender = 2)
    {
        Console.WriteLine ("Age: " + age);
        Console.WriteLine ("Height:" + height);
        Console.WriteLine ("Weight:" + weight);
        Console.WriteLine ("Gender:" + gender);

        Console.WriteLine ("Your BMR is " + 655 + (4.35 * weight) + (4.7 * height) - (4.7 * age));
    }
}

1 个答案:

答案 0 :(得分:0)

您需要将常量值转换为十进制并在计算周围放置一个括号。

public static void BMR(string[] args)
{
    int weight, height, age, gender;

    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());

    if (gender == 1)
    {
        Console.WriteLine("Age: " + age);
        Console.WriteLine("Height:" + height);
        Console.WriteLine("Weight:" + weight);
        Console.WriteLine("Gender:" + gender);

        Console.WriteLine("Your BMR is" + (66.0m + (6.23m * weight) + (12.7m * height) - (6.8m * age)));
    }
    else
    {
        Console.WriteLine("Age: " + age);
        Console.WriteLine("Height:" + height);
        Console.WriteLine("Weight:" + weight);
        Console.WriteLine("Gender:" + gender);

        Console.WriteLine("Your BMR is " + (655.0m + (4.35m * weight) + (4.7m * height) - (4.7m * age)));
    }
}