【问题标题】:why is the output is always 0? [duplicate]为什么输出总是0? [复制]
【发布时间】:2018-11-30 15:40:45
【问题描述】:

今天我开始在 Visual Studio 2017 中使用 C# 和 Windows 窗体创建一个项目。该程序应计算具有矩形底面的金字塔的体积并显示结果。

问题是,如果我单击应该显示结果的按钮,结果为 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?

【问题讨论】:

  • @BradleyDotNET 自从几个月前我获得金牌后,我就已经有了,所以我不知道

标签: c# winforms textbox label


【解决方案1】:

double Total = 1/3 * h * a * b; 行中,1/3 是整数除法,结果为 0。将其更改为 1.0 / 3.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多