当我尝试使用if语句分配值将值分配给变量时,使用未分配的变量错误

发布于 2025-01-22 04:54:16 字数 1282 浏览 0 评论 0 原文

我正在用Windows表单编写一个程序,该程序试图使用if语句将值分配给变量,这取决于单击表单中的哪些radiobuttons。我在以下行中以粗体以“使用无分配变量”的错误。问题是 totalweeks 显然没有被分配,但是我在上面的if语句中为其分配了一个值。谁能提出一个可能的修复或想法来试图解决问题?

private void button1_Click(object sender, EventArgs e)
{
    //Assigning variables
    int WeeklyRate; //To assign integer values for "WeeklyRate" for future calculation use. If Radio button clicked it has value of radio button etc. 
    if (radioButton1.Checked)
    {
        WeeklyRate = 10;
    }
    else if (radioButton2.Checked)
    {
        WeeklyRate = 15;
    }
    else if (radioButton3.Checked)
    {
        WeeklyRate = 20;
    }

    int TotalWeeks;//assign values to total weeks based on number of months selected, to use in calculations for total cost.
    if (radioButton4.Checked)
    {
        TotalWeeks = 12;
    }
    else if (radioButton5.Checked)
    {
        TotalWeeks = 52;
    }
    else if (radioButton6.Checked)
    {
        TotalWeeks = 104;
    }

    int Payments;//assigning the amount of payments based on frequency radiobutton selected. 
    if (radioButton7.Checked)
    {
        **Payments = TotalWeeks;**
    }
    else if (radioButton8.Checked)
    {
        **Payments = TotalWeeks / 4;**
    }
}
}
}

I am writing a program in Windows Forms which I am trying to assign values to variables using IF statements depending on which radiobuttons in the form are clicked. I am getting an error "use of unassigned variable" on the lines below in bold. The issue is that TotalWeeks has apparently not been assigned, however I assigned it a value in the IF statement above that one. Can anyone suggest a possible fix or idea to try and sort out the issue?

private void button1_Click(object sender, EventArgs e)
{
    //Assigning variables
    int WeeklyRate; //To assign integer values for "WeeklyRate" for future calculation use. If Radio button clicked it has value of radio button etc. 
    if (radioButton1.Checked)
    {
        WeeklyRate = 10;
    }
    else if (radioButton2.Checked)
    {
        WeeklyRate = 15;
    }
    else if (radioButton3.Checked)
    {
        WeeklyRate = 20;
    }

    int TotalWeeks;//assign values to total weeks based on number of months selected, to use in calculations for total cost.
    if (radioButton4.Checked)
    {
        TotalWeeks = 12;
    }
    else if (radioButton5.Checked)
    {
        TotalWeeks = 52;
    }
    else if (radioButton6.Checked)
    {
        TotalWeeks = 104;
    }

    int Payments;//assigning the amount of payments based on frequency radiobutton selected. 
    if (radioButton7.Checked)
    {
        **Payments = TotalWeeks;**
    }
    else if (radioButton8.Checked)
    {
        **Payments = TotalWeeks / 4;**
    }
}
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2025-01-29 04:54:16

radiobutton4 > radiobutton5 , radiobutton6 均未被检查时,您不能将其除以4

。变量

int TotalWeeks = 0;

(结果:<代码>付款在未检查radiobuttons时将为0)


或在初始化时

int? TotalWeeks = null;

,在分配付款金额时

if (radioButton7.Checked && TotalWeeks is not null)
{
    Payments = TotalWeeks;
}
else if (radioButton8.Checked && TotalWeeks is not null)
{
    Payments = TotalWeeks / 4;
}

(结果:现在:现在付款变量,都没有分配,无分配当没有检查radiobuttons时)

编辑:谢谢 gserg ,我忘记了之后,我忘了 > int 使其无效

When neither of radioButton4, radioButton5, radioButton6 is checked, then you can't for example divide it by 4.

You should use this when initializing variable

int TotalWeeks = 0;

(result: Payments will be 0 when radioButtons aren't checked)


or this when initializing

int? TotalWeeks = null;

and this when assigning the amount of payments

if (radioButton7.Checked && TotalWeeks is not null)
{
    Payments = TotalWeeks;
}
else if (radioButton8.Checked && TotalWeeks is not null)
{
    Payments = TotalWeeks / 4;
}

(result: Now Payments variable is unassigned, when radioButtons aren't checked)

Edit: Thanks GSerg, I forgot about ? after int to make it nullable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文