我的IF语句在Windows窗体应用程序中不起作用

时间:2019-05-09 02:15:51

标签: c# if-statement windows-forms-designer

我正在尝试创建具有各种单选按钮作为不同选项的体育馆会员资格。我希望用户能够选择计算按钮以及在文本框中显示的费用,但是由于某种原因,如果我声明将所有单选按钮费用加在一起而不是仅由用户选择的话,那么我就可以了。 >

private void calculatebutton_click(object sender, eventargs e)
{
int membershipcost = 0;

if (basicradiobutton.checked);
membershipcost += 10;

if (regularradiobutton.checked);
membershipcost += 15;

if (premiumradiobutton.checked);
membershipcost += 20;

membershipcosttxtbx.text = membershipcost.tostring();

2 个答案:

答案 0 :(得分:1)

您需要从if语句中删除分号,或像这样设置代码格式:

private void calculatebutton_click(object sender, eventargs e)
{
    int membershipcost = 0;
    if (basicradiobutton.checked)
    {
        membershipcost += 10;
    }
    if (regularradiobutton.checked)
    {
        membershipcost += 15;
    }
    if (premiumradiobutton.checked)
    {
        membershipcost += 20;
    }
    membershipcosttxtbx.text = membershipcost.tostring();
}

答案 1 :(得分:0)

您的C#编写不正确。我建议使用{}来定义与if条件相关的代码块。您可以创建一行if语句,该语句已包含在第二个if中。将;放在if(...)之后将导致该语句不执行任何操作。

private void calculatebutton_click(object sender, eventargs e)
{
    int membershipcost = 0;

    if (basicradiobutton.checked)
    {
        membershipcost += 10;
    }

    if (regularradiobutton.checked) membershipcost += 15;

    if (premiumradiobutton.checked)
    {
        membershipcost += 20;
    }

    membershipcosttxtbx.text = membershipcost.tostring();
}