我正在尝试使用 c# 创建一个 BMI 计算器。
My formula is BMI = (weight /(height * height)) * factor.
Example: BMI = (172 / (73 * 73 )) * 703
BMI = (172 / 5329) * 703
BMI = ( 0.032) * 703
BMI = 22.69
我的问题是,在我将体重除以高度之后,我得到了一个分数,尽管当我将它乘以它大于 1 的因子时,我无法将它保存在我的整数变量中。所以我的总 BMI 总是为 0。
我需要重新考虑我的数据类型吗?还是我应该转换它们?我不确定如何解决这个问题。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Calculate_Click(object sender, EventArgs e)
{
int feet, inches, height, pounds, result, factor;
factor = 703;
//convert the text input to the correct data type
feet = Convert.ToInt32(feetBox.Text);
inches = Convert.ToInt32(inchesBox.Text);
pounds = Convert.ToInt32(poundsBox.Text);
//turn the feet and inches into a single height value in inches
height = (feet * 12) + inches;
//calculate the BMI
//result = (pounds / (height * height) ) * factor;
result = factor * (pounds / (height * height));
// output the results
output.Text = String.Format("Your BMI is: {0}", result);
}
}