我有这个程序,我正在上课,我认为问题出在我的if语句中。当我运行程序并进行选择并点击总按钮时,我会在订单标签中显示“为您的开胃菜,主菜和甜点”以及订单总标签中的牛排晚餐价格。我想我可能不得不使用switch语句,但我不确定任何建议会有很大的帮助,谢谢。
namespace Restaurant
{
public partial class frmRestaurant : Form
{
decimal AppetizerPrice = 0.0m;
decimal EntreePrice = 0.0m;
decimal DessertPrice = 0.0m;
decimal total = 0.0m;
string AppetizerOrder = "", EntreeOrder = "", DessertOrder = "", order = "";
public frmRestaurant()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnTotal_Click(object sender, EventArgs e)
{
CalculateTotal();
Order();
lblOrder.Text = order;
lblTotal.Text = total.ToString();
}
private void grpAppetizer_Enter(object sender, EventArgs e)
{
if (radCheeseSticks.Checked)
{
AppetizerPrice = 5.99m;
AppetizerOrder = "Cheese Sticks";
}
else if (radGarlicBread.Checked)
{
AppetizerPrice = 4.50m;
AppetizerOrder = "Garlic Bread";
}
else if (radChipsnSalsa.Checked)
{
AppetizerPrice = 3.50m;
AppetizerOrder = "Chips and Salsa";
}
}
private void grpEntree_Enter(object sender, EventArgs e)
{
if (radSteakDinner.Checked)
{
EntreePrice = 12.50m;
EntreeOrder = "Steak Dinner";
}
else if (radChickenParm.Checked)
{
EntreePrice = 10.99m;
EntreeOrder = "Chicken Parmigiana";
}
else if (radChipsnSalsa.Checked)
{
EntreePrice = 3.50m;
EntreeOrder = "Chips and Salsa";
}
}
private void grpDessert_Enter(object sender, EventArgs e)
{
if (radSteakDinner.Checked)
{
DessertPrice = 12.50m;
DessertOrder = "Steak Dinner";
}
else if (radChickenParm.Checked)
{
DessertPrice = 10.99m;
DessertOrder = "Chicken Parmigiana";
}
else if (radChipsnSalsa.Checked)
{
DessertPrice = 3.50m;
DessertOrder = "Chips and Salsa";
}
}
public decimal CalculateTotal()
{
total = AppetizerPrice + EntreePrice + DessertPrice;
return total;
}
public string Order()
{
order = AppetizerOrder + "for your appetizer," + EntreeOrder + "for your entree, and " + DessertOrder + "for dessert";
return order;
}
}
}
答案 0 :(得分:1)
我认为GroupBox.Enter
事件对您的用例没有用处。在控件激活期间的某个时间调用enter事件,但在值更改时不调用。
解决问题的一种方法是仅在点击“总计”按钮时设置开胃菜/主菜/甜点价格和文字。您现在不需要在当前表单中使用它。
另一种解决方法是使用正确的事件:只需处理所有这些单选按钮的RadioButton.CheckedChanged
事件,例如:
private void radGarlicBread_CheckedChanged(object sender, EventArgs e)
{
if (radGarlicBread.Checked)
{
AppetizerPrice = 4.50m;
AppetizerOrder = "Garlic Bread";
}
}
答案 1 :(得分:1)
首先,您可能想要使用String.Format()
,因为它看起来比一堆连接更清晰 - 它还可以帮助您捕捉诸如此处缺少空格之类的内容(您的输出看起来好像是,例如'Chips and Salsafor your appetizer ...')
最好只找到Order
方法中检查的项目,而不是每次用户检查时都更新它。
我不确定出了什么问题,但是每次调用每个Debug.WriteLine()
方法时,你都可以做Enter()
看看发生了什么。
例如:
Debug.WriteLine("grpDesert_Enter");
Debug.WriteLine(radSteakDinner.Checked);
Debug.WriteLine(radChickenPark.Checked);
Debug.WriteLine(radChipsnSalsa.Checked);