如果没有选择单选按钮,如何停止计算?

时间:2016-09-17 16:47:57

标签: c# winforms

我真的很喜欢编程。 C#是我参加的第一堂课,我坚持这个项目。我们必须创建一个程序,在选择车间单选按钮和位置单选按钮后计算车间成本。除了一件事,我已经按照预期的方式工作了。

假设您选择了一个研讨会,但没有选择一个位置。我有一个false会出现说“选择一个位置”的地方,但如果发生这种情况,如何阻止程序计算呢?截至目前,它只会计算并给出位置数量0.我需要它根本不计算。

MessageBox

3 个答案:

答案 0 :(得分:1)

您必须退出该方法。在else块中添加了return语句。

private void btncalc_Click(object sender, EventArgs e)
{
    int wsregistration = 0;
    int lcost = 0;
    const decimal DAYS = 3;


    //For the following if statements, depending on what workshop and location is selected,
    //their correstponding registration and lodging fees will be displayed
    if (rbtHandlingStress.Checked == true)
    {
        wsregistration = 1000;
    }
    else if (rbtSupervisionSkills.Checked == true)
    {
        wsregistration = 1500;
    }
    else if (rbtTimeManagement.Checked == true)
    {
        wsregistration = 800;
    }

    else
    {       
        lblTotalCost.Text = "";
        lblLodgingCost.Text = "";
        lblRegistrationCost.Text = "";
        MessageBox.Show("Please Select a Workshop");
        return;
    }


    if (rbtAustin.Checked == true)
    {
        lcost = 150;
    }
    else if (rbtChicago.Checked == true)
    {
        lcost = 225;
    }
    else if (rbtDallas.Checked == true)
    {
        lcost = 175;
    }
    else
    {       
        lblRegistrationCost.Text = " ";
        lblTotalCost.Text = " ";
        lblLodgingCost.Text = " ";
        MessageBox.Show("Please Select a Location");
        return;
    }

    lblRegistrationCost.Text = wsregistration.ToString("C");
    lblLodgingCost.Text = lcost.ToString("C");
    lblTotalCost.Text = (wsregistration + (lcost * DAYS)).ToString("C");
}

答案 1 :(得分:0)

在函数内的任何地方写一个“return”退出该函数,也许在你显示输入位置的消息框之后,你输入

return;

这应该可以胜任。

答案 2 :(得分:0)

在显示如下所示的消息框

之后,只需在代码中添加return语句
library(dplyr)

df <- data.frame(year = rep(16, 7),
                 month = c("June", "June", "July", "July", "Aug", "Aug", "Sept"),
                 NPS = c(8, 3, 4, 10, 3, 1, 8))


summarise(group_by(df, year, month), sum(NPS))

}