当我尝试使用if语句分配值将值分配给变量时,使用未分配的变量错误
我正在用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;**
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当
radiobutton4
> radiobutton5 ,radiobutton6
均未被检查时,您不能将其除以4。变量
(结果:<代码>付款在未检查radiobuttons时将为0)
或在初始化时
,在分配付款金额时
(结果:现在:现在
付款
变量,都没有分配,无分配当没有检查radiobuttons时)编辑:谢谢 gserg ,我忘记了
之后,我忘了
使其无效?
> intWhen neither of
radioButton4
,radioButton5
,radioButton6
is checked, then you can't for example divide it by 4.You should use this when initializing variable
(result:
Payments
will be 0 when radioButtons aren't checked)or this when initializing
and this when assigning the amount of payments
(result: Now
Payments
variable is unassigned, when radioButtons aren't checked)Edit: Thanks GSerg, I forgot about
?
afterint
to make it nullable