if用radiobuttons进行重构。
有两个无线电按钮可以给出不同的价格。 示例:选择Elektric2和3天的租金。电动自行车= 200欧元 +每天30欧元(200 EU +(30*3))。
我该如何重构,因为我对所有选项都有60多行代码。
这是整个代码的一些:
if (radioButtonElectric2.Checked && radioButtonADay.Checked)
{
electric2 = electric2 + 1 * 30;
labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectric2.Checked && radioButtonThreeDays.Checked)
{
electric2 = electric2 + 3 * 30;
labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectric2.Checked && radioButtonSevenDays.Checked)
{
electric2 = electric2 + 7 * 30;
labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectrical2.Checked && radioButtonFourteenDays.Checked)
{
electric2 = electric2 + 14 * 30;
labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
There are two radio buttons which give different prices.
Example: elektric2 is selected and 3 days rent. Electric bike = 200 euro + 30 euro per day (200 eu + (30*3)).
How can I refactor this because I have 60+ lines of code for all the options.
Here is a bit of the entire code:
if (radioButtonElectric2.Checked && radioButtonADay.Checked)
{
electric2 = electric2 + 1 * 30;
labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectric2.Checked && radioButtonThreeDays.Checked)
{
electric2 = electric2 + 3 * 30;
labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectric2.Checked && radioButtonSevenDays.Checked)
{
electric2 = electric2 + 7 * 30;
labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
else if (radioButtonElectrical2.Checked && radioButtonFourteenDays.Checked)
{
electric2 = electric2 + 14 * 30;
labelSubTotal.Text = "Subtotal:€" + electric2.ToString();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方程式基本上归结为三分。
将这三件事分为单个功能使您的代码变得更加简单。
最后,最终代码要简单得多:
Your equation basically boils down to three amounts.
Splitting these three things into individual functions makes your code much simpler.
In the end the final code is much simpler: