我怎样才能让这个程序一次检查多项内容?
/在此代码的底部有两个 if 语句。我希望能够首先检查用户是否选择了一种口味(默认情况下,口味文本框的文本是“选择一种口味”)。如果他们在没有选择口味的情况下单击“AddDrink”按钮,则消息框需要显示他们尚未选择口味,而不是尚未输入数量,然后继续执行程序。但是,如果他们选择了口味但没有输入数量,那么它应该显示有关输入数量的消息框。如果需要更多信息,请告诉我。感谢您的帮助!!!/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Chap9DrinkApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ComputeCost_CheckedChanged(object sender, EventArgs e)
{
}
private void btnCompleteOrder_Click(object sender, EventArgs e)
{
MessageBox.Show(lblFinalFlavor.Text + "\n" + lblFinalTopping.Text + "\n" + lblFinalCost.Text);
}
private void btnAddDrink_Click(object sender, EventArgs e)
{
double cost = 0;
double quantityPrice;
double quantityNum;
this.lblFinalTopping.Text = "Extras: ";
if (this.radSmall.Checked)
{
cost += 3.00;
}
else if (this.radMedium.Checked)
{
cost += 3.50;
}
else if (this.radLarge.Checked)
{
cost += 4.00;
}
if (this.ckBoxCherries.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Cherries ";
}
if (this.ckBoxGrape.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Grapes ";
}
if (this.ckBoxLemon.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Lemon ";
}
if (this.ckBoxPineapple.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Pineapple ";
}
if (this.ckBoxStrawberry.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Strawberry ";
}
if (this.ckBoxVitamin.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Vitamin Pack ";
}
if (this.ckBoxWhipped.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Whipped cream ";
}
this.lblFinalCost.Visible = true;
if (this.txtQuantity.Text != "0")
{
quantityNum = double.Parse(this.txtQuantity.Text);
quantityPrice = cost * quantityNum;
this.lblFinalCost.Text = "Cost: " + quantityPrice.ToString("c");
}
else
MessageBox.Show("Please enter a quantity!");
if (this.cmboBoxJuiceFlav.Text != "Select a flavor")
this.lblFinalFlavor.Text = "Flavor: " + this.cmboBoxJuiceFlav.Text;
else
MessageBox.Show("Please select a flavor!");
}
}
}
/At the bottom of this code there are two if statements. I want to be able to check FIRST whether the user has selected a flavor(the text for the flavor textbox is "Select a flavor" by default). If they click the AddDrink button without selecting a flavor, the message box needs to show that they haven't selected a flavor, instead of haven't entered a quantity, and then proceed with the program. However, if they have chosen a flavor but haven't entered a quantity then it should show the message box about entering a quantity. If anymore info is needed please let me know. THANKS FOR YOUR HELP!!!/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Chap9DrinkApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ComputeCost_CheckedChanged(object sender, EventArgs e)
{
}
private void btnCompleteOrder_Click(object sender, EventArgs e)
{
MessageBox.Show(lblFinalFlavor.Text + "\n" + lblFinalTopping.Text + "\n" + lblFinalCost.Text);
}
private void btnAddDrink_Click(object sender, EventArgs e)
{
double cost = 0;
double quantityPrice;
double quantityNum;
this.lblFinalTopping.Text = "Extras: ";
if (this.radSmall.Checked)
{
cost += 3.00;
}
else if (this.radMedium.Checked)
{
cost += 3.50;
}
else if (this.radLarge.Checked)
{
cost += 4.00;
}
if (this.ckBoxCherries.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Cherries ";
}
if (this.ckBoxGrape.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Grapes ";
}
if (this.ckBoxLemon.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Lemon ";
}
if (this.ckBoxPineapple.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Pineapple ";
}
if (this.ckBoxStrawberry.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Strawberry ";
}
if (this.ckBoxVitamin.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Vitamin Pack ";
}
if (this.ckBoxWhipped.Checked)
{
cost += .50;
this.lblFinalTopping.Text += "Whipped cream ";
}
this.lblFinalCost.Visible = true;
if (this.txtQuantity.Text != "0")
{
quantityNum = double.Parse(this.txtQuantity.Text);
quantityPrice = cost * quantityNum;
this.lblFinalCost.Text = "Cost: " + quantityPrice.ToString("c");
}
else
MessageBox.Show("Please enter a quantity!");
if (this.cmboBoxJuiceFlav.Text != "Select a flavor")
this.lblFinalFlavor.Text = "Flavor: " + this.cmboBoxJuiceFlav.Text;
else
MessageBox.Show("Please select a flavor!");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是你想要的吗:
Isn't this what you want:
有更好的方法来完成您想做的事情。但为了解决你的问题,你可以尝试这个 -
There's better ways to do what you're trying to do. But just to get your problem solved, you could try this -