if if语句首先停止IF

时间:2016-10-05 15:27:00

标签: c#

我的代码遇到了一些问题.. C# 如果我运行它,它会停在第一个If语句...

尝试并寻找更多答案,但没有找到任何有效的方法

       private void Bereken_Click(object sender, EventArgs e)
    {
        // Afmetingen van het het blok
        var PL = int.Parse(EigenML.Text);
        var PB = int.Parse(EigenMB.Text);
        var PH = int.Parse(EigenMH.Text);

        // Afmetingen van het stuk
        var L = int.Parse(StukML.Text);
        var B = int.Parse(StukMB.Text);
        var H = int.Parse(StukMH.Text);
        LabelEigenML.Text = PL.ToString();
        LabelEigenMB.Text = PB.ToString();
        LabelEigenMH.Text = PH.ToString();

        var L1 = PL / L;
        var B1 = PB / B;
        var H1 = PH / H;
        var T1 = L1 * B1 * H1;

        var L2 = PL / L;
        var B2 = PB / B;
        var H2 = PH / H;
        var T2 = L2 * B2 * H2;

        var L3 = PL / L;
        var B3 = PB / B;
        var H3 = PH / H;
        var T3 = L3 * B3 * H3;

        var L4 = PL / L;
        var B4 = PB / B;
        var H4 = PH / H;
        var T4 = L4 * B4 * H4;

        var L5 = PL / L;
        var B5 = PB / B;
        var H5 = PH / H;
        var T5 = L5 * B5 * H5;

        var L6 = PL / L;
        var B6 = PB / B;
        var H6 = PH / H;
        var T6 = L6 * B6 * H6;

        if (T1 >= T2 && T1 >= T3 && T1 >= T4 && T1 >= T5 && T1 >= T6) {
            LabelStukML.Text = "Lengte" + L.ToString();
            LabelStukMB.Text = "Breedte" + B.ToString();
            LabelStukMH.Text = "Hoogte" + H.ToString();
            labelTotaal.Text = T1.ToString();
            Console.WriteLine("Should be 1");
        } else if (T2 >= T1 && T2 >= T3 && T2 >= T4 && T2 >= T5 && T2 >= T6)
        {
            LabelStukML.Text = "Lengte" + L.ToString();
            LabelStukMB.Text = "Hoogte" + H.ToString();
            LabelStukMH.Text = "Breedte" + B.ToString();
            labelTotaal.Text = T2.ToString();
            Console.WriteLine("Should be 2");

        } else if (T3 >= T1 && T3 >= T2 && T3 >= T4 && T3 >= T5 && T3 >= T6)
        {
            LabelStukML.Text = "Breedte" + B.ToString();
            LabelStukMB.Text = "Lengte" + L.ToString();
            LabelStukMH.Text = "Hoogte" + H.ToString();
            labelTotaal.Text = T3.ToString();

            Console.WriteLine("Should be 3");


        }
        else if (T4 >= T1 && T4 >= T2 && T4 >= T3 && T4 >= T5 && T4 >= T6)
        {
            LabelStukML.Text = "Breedte" + B.ToString();
            LabelStukMB.Text = "Hoogte" + H.ToString();
            LabelStukMH.Text = "Lengte" + L.ToString();
            labelTotaal.Text = T4.ToString();
            Console.WriteLine("Should be 4");


        }
        else if (T5 >= T1 && T5 >= T2 && T5 >= T3 && T5 >= T4 && T5 >= T6)
        {
            LabelStukML.Text = "Hoogte" + H.ToString();
            LabelStukMB.Text = "Breedte" + B.ToString();
            LabelStukMH.Text = "Lengte" + L.ToString();
            labelTotaal.Text = T5.ToString();
            Console.WriteLine("Should be 5");


        }
        else if (T6 >= T1 && T6 >= T2 && T6 >= T3 && T6 >= T4 && T6 >= T5)
        {

            LabelStukML.Text = "Hoogte" + H.ToString();
            LabelStukMB.Text = "Lengte" + L.ToString();
            LabelStukMH.Text = "Breedte" + B.ToString();
            labelTotaal.Text = T6.ToString();
            Console.WriteLine("Should be 6");

        }
        else
        {

            LabelStukML.Text = "Error";
            LabelStukMB.Text = "Error" ;
            LabelStukMH.Text = "Error";
            labelTotaal.Text = "Error";
            Console.WriteLine("Should be 6");

        }







    }
}

}

它应该计算某些内容,然后查看最大的内容并按特定顺序打印这些内容,但我总是得到我的第一个IF语句的输出

1 个答案:

答案 0 :(得分:1)

我看到两个问题:

  • 所有6个计算块都包含完全相同的公式(正如其他人也指出的那样)。
  • 您正在进行整数数学运算,这意味着结果也是整数,对于某些输入,它们都可以全部舍入为0,例如:如果PL < L.修复:改为使用ListView