为什么输出总是0?
why is the output is always 0?
今天,我在 Visual Studio 2017 年开始使用 C# 使用 Windows Forms 创建一个项目。该程序应计算具有矩形底面的金字塔的体积并显示结果。
问题是,如果我点击应该显示结果的按钮,结果是 0。
这里是重要的代码:
// stores height h, side1 a, side2 b
double a, b, h;
//Reads out b from a textbox
private void txbB_TextChanged(object sender, EventArgs e)
{
string parseB = txbB.Text;
b = double.Parse(parseB);
}
//Reads out a from a textbox
private void txbA_TextChanged(object sender, EventArgs e)
{
string parseA = txbA.Text;
a = double.Parse(parseA);
}
//Reads out h from a textbox
private void txbH_TextChanged(object sender, EventArgs e)
{
string parseH = txbH.Text;
h = double.Parse(parseH);
}
//button which calculates the volume of the pyramid
//when clicked and prints it to the label "LblErgebnis"
private void Cmdrechnen_Click(object sender, EventArgs e)
{
double Total = 1/3 * h * a * b;
string Result = Total.ToString();
LblErgebnis.Text = Result;
}
有人能告诉我为什么结果总是 0 吗?
行double Total = 1/3 * h * a * b;
中,1/3
是整数除法,结果为0。将其更改为1.0 / 3.0
。
今天,我在 Visual Studio 2017 年开始使用 C# 使用 Windows Forms 创建一个项目。该程序应计算具有矩形底面的金字塔的体积并显示结果。
问题是,如果我点击应该显示结果的按钮,结果是 0。
这里是重要的代码:
// stores height h, side1 a, side2 b
double a, b, h;
//Reads out b from a textbox
private void txbB_TextChanged(object sender, EventArgs e)
{
string parseB = txbB.Text;
b = double.Parse(parseB);
}
//Reads out a from a textbox
private void txbA_TextChanged(object sender, EventArgs e)
{
string parseA = txbA.Text;
a = double.Parse(parseA);
}
//Reads out h from a textbox
private void txbH_TextChanged(object sender, EventArgs e)
{
string parseH = txbH.Text;
h = double.Parse(parseH);
}
//button which calculates the volume of the pyramid
//when clicked and prints it to the label "LblErgebnis"
private void Cmdrechnen_Click(object sender, EventArgs e)
{
double Total = 1/3 * h * a * b;
string Result = Total.ToString();
LblErgebnis.Text = Result;
}
有人能告诉我为什么结果总是 0 吗?
行double Total = 1/3 * h * a * b;
中,1/3
是整数除法,结果为0。将其更改为1.0 / 3.0
。