为什么它仍然显示我的结果

时间:2016-05-02 07:05:58

标签: c#

在这部分中,如果我的腰围小于60且身高大于120,则会显示结果框。但是当腰围大于60且身高小于120时,结果将无法显示。如何阻止第一种情况发生,应该显示结果。

            if (this.Male.Checked) {
            if (waist < 60) {
                MessageBox.Show("Your waist is too low to calculate, please enter a higer numer");
            }

            if (height < 120) {
                MessageBox.Show("Your height is too low to calculate, please enter a higer numer");
            }
            else{

                if (ratio < Male_Risk) {
                    MessageBox.Show("Your ratio is: " + ratio + "\n" + "your raito is at low risk, please be careful");

                } 
                else {
                    MessageBox.Show("Your ratio is: " + ratio + "\n" + "Warning : your raito is at high risk, please save yourself!");

                }

            }
        }

1 个答案:

答案 0 :(得分:1)

您检查腰围和身高的if陈述没有联系在一起。因此,如果您的患者腰围大于60但身高低于120,那么您的结果将不会显示,因为您所拥有的第二个if语句与上述if语句无关。这意味着只有当高度大于120时才会显示else部分。

只需将if(height < 120)更改为else if(height < 120)

即可

像这样:

if (this.Male.Checked) 
{
    if (waist < 60) 
    {
        MessageBox.Show("Your waist is too low to calculate, please enter a higer numer");
    }

    else if (height < 120) 
    {
        MessageBox.Show("Your height is too low to calculate, please enter a higer numer");
    }
    else 
    {
        if (ratio < Male_Risk) 
        {
            MessageBox.Show("Your ratio is: " + ratio + "\n" + "your raito is at low risk, please be careful");
        } 
        else 
        {
            MessageBox.Show("Your ratio is: " + ratio + "\n" + "Warning : your raito is at high risk, please save yourself!");
        }
    }
}

如果你想同时显示MessageBox,你需要将腰围和身高检查分开if,如下所示:

if(waist < 60 || height < 120) 
{
    if (waist < 60) 
    {
        MessageBox.Show("Your waist is too low to calculate, please enter a higer numer");
    }
    if (height < 120) 
    {
        MessageBox.Show("Your height is too low to calculate, please enter a higer numer");
    }
}
else 
{
     // Put your ratio results here...
}