谁能帮我检查我的BMI计算器? (C#) [英] Can anyone help me check my BMI Calculator? (C#)

查看:162
本文介绍了谁能帮我检查我的BMI计算器? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一周前才刚开始编程,而我的第一个任务是编写BMI计算器。

I just started programming a week ago and my first assignment was to code a BMI calculator.

启动时应该看起来像这样:

It is supposed to look like this when launched:

    BMI Calculator
    Your weight in kg: x 
    Your height in cm: x
    Gender (m/f): x

-> You are underweight/normal/overweight

到目前为止,这是我的代码:

Here is my code so far:

            Console.WriteLine("BMI Calculator");
            Console.WriteLine("===========");
            Console.WriteLine();

            Console.Write("Weight in kg: ");
            int kg;
            kg = Convert.ToInt32(Console.ReadLine());

            Console.Write("Height in cm: ");
            int m;
            m = Convert.ToInt32(Console.ReadLine());

            Console.Write("Gender (m/f):");
            string Geschlecht = Console.ReadLine();

            int BMI;
            BMI = kg / (m / 100) * (m / 100);

            if (BMI < 19 & Gender == "f")
            { Console.WriteLine("-> Underweight"); }
            if (BMI >= 19 & BMI <= 24 & Gender == "f") 
            { Console.WriteLine("-> Normal"); }
            if (BMI > 24 & Geschlecht == "f")
            { Console.WriteLine("-> Overweight"); }

            if (BMI < 20 & Gender == "m")
            { Console.WriteLine("-> Underweight"); }
            if (BMI >= 20 & BMI <= 25 & Gender == "m")
            { Console.WriteLine("-> Normal"); }
            if (BMI > 25 & Gendert == "m")
            { Console.WriteLine("-> Overweight"); }

            Console.ReadLine();

我不确定我的代码出了什么问题,但是每当我输入60公斤,170厘米和男性时,我变得超重,即使我应该恢复正常。

I'm not sure what's wrong with my code but whenever I enter 60kgs, 170cm and male, I get overweight, even though I should get normal. Same thing with anything above 10kgs actually.

PS:我真的是编程的初学者,所以我对编程语言的命令深表歉意。

PS: I'm really a beginner at programming so I apologize for my command of the programming lingo.

为方便起见:

http://i.stack.imgur.com/admqr.png

提前感谢!

推荐答案

当您这样做:

BMI = kg / (m / 100) * (m / 100);

m int ,您将进行整数除法,在这种情况下 170/100 = 1 。正如User1551892所指出的那样,您需要对计算顺序更加具体。

And m is an int, you'd be doing integer division, in which case 170 / 100 = 1. And as User1551892 pointed out, you'll need to be a bit more specific about the order of calculations.

尝试:

double BMI = kg / ( ( m / 100.0 ) * ( m / 100.0 ) );

这将迫使它执行浮点除法,并应获得更好的结果。

That will force it to do floating point division and should get you better results.

另外,您可以使用 Math.Pow 来避免 m / 100.0 两次:

Also, you could use Math.Pow to avoid having m / 100.0 twice:

double BMI = kg / Math.Pow( m / 100.0, 2 );

这篇关于谁能帮我检查我的BMI计算器? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆