gpt4 book ai didi

C# 值始终为 0 控制台应用程序

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:15 25 4
gpt4 key购买 nike

我不知道为什么我的 BMI 值总是 = 0。我是编程新手,我错过了什么?除此之外,我的 if 语句还好吗?我错过了什么?

    static void Main(string[] args) {
double WeightKg = 0.0, HeightCm = 0.0, Weightlbs = 0.0, WeightOz = 0.0, BMI = 0.0, Feet = 0.0, Inches = 0.0;
int BMIOption;
string AnotherConversion;

string BMIMenu = ("Which Measurement You Want to use to enter the weight and height?"
+ "\n1)Enter 1 for Metric"
+ "\n2)Enter 2 for British Imperial:");
Console.Write(BMIMenu);
BMIOption = int.Parse(Console.ReadLine());

if (BMIOption == 1) {
Console.Write("\nPlease Enter your Weight in Kilogram (kg):");
WeightKg = int.Parse(Console.ReadLine());
Console.Write("\nPlease Enter your Height in in centimetres (cm):");
HeightCm = int.Parse(Console.ReadLine());

BMI = WeightKg / (HeightCm * HeightCm);

if (BMI >= 35) {
Console.WriteLine("\nYour BMI is {0:C},Severe Obesity", BMI);
} else if (BMI >= 30) {
Console.WriteLine("\nYour BMI is {0:C},Obese", BMI);
} else if (BMI >= 25) {
Console.WriteLine("\nYour BMI is {0:C},OverWeight", BMI);
} else if (BMI >= 18.5) {
Console.WriteLine("\nYour BMI is {0:C},Healthy BodyWeight", BMI);
} else if (BMI <= 18.5) {
Console.WriteLine("\nYour BMI is {0:C},UnderWeight", BMI);
}//End if

Console.Write("\nWould you like to make an another conversion? \n\n(Enter Y to make an another conversion/Enter any other key to exit):");

Console.ReadKey();

最佳答案

BMI 的计算单位是米,而不是厘米。因此,您需要将 HeightCm 转换为 HeightM。如果你不这样做,你会得到非常小的数字,然后打印为 0。

double HeightM = HeightCm / 100.0;
BMI = WeightKg / (HeightM * HeightM);

此外,在解析时,使用 double.Parse 而不是 int.Parse。现在的方式是,您将只解析没有小数部分的数字。

关于C# 值始终为 0 控制台应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22588542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com