【发布时间】:2019-05-21 14:39:24
【问题描述】:
我正在尝试编写一个非常简单的程序来计算液体尼古丁强度。基本上是(strengh / nicStrengh) * amount。它总是以0 出现。
private void lblCalculate_Click(object sender, EventArgs e)
{
int strengh = Convert.ToInt32(txtBoxDesiredStrengh.Text);
int nicStrengh = Convert.ToInt32(txtBoxNicStrengh.Text);
int amount = Convert.ToInt32(txtBoxAmount.Text);
int result = strengh / nicStrengh * amount;
string resultStr = result.ToString();
label1.Text = resultStr;
}
【问题讨论】:
-
strengh / nicStrengh- 整数除法 - 写成amount * strengh / nicStrengh; -
尝试使用
float而不是int。 -
整数除法不会四舍五入,它会一直截断。因此,任何小于 1 的绝对分数始终为 0。
标签: c# calculation