我如何让这个程序一次检查多个东西
本文关键字:一次 检查 程序 | 更新日期: 2023-09-27 18:11:00
/在这段代码的底部有两个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!");
}
}
}
这不是你想要的吗:
if (this.cmboBoxJuiceFlav.Text != "Select a flavor")
{
this.lblFinalFlavor.Text = "Flavor: " + this.cmboBoxJuiceFlav.Text;
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!");
}
}
else
{
MessageBox.Show("Please select a flavor!");
}
有更好的方法来做你想做的事。但是为了解决你的问题,你可以试试这个-
.
.
.
string msgBoxTxt = "";
if (this.txtQuantity.Text != "0")
{
quantityNum = double.Parse(this.txtQuantity.Text);
quantityPrice = cost * quantityNum;
this.lblFinalCost.Text = "Cost: " + quantityPrice.ToString("c");
}
else
msgBoxTxt += "Please enter a quantity! ";
if (this.cmboBoxJuiceFlav.Text != "Select a flavor")
this.lblFinalFlavor.Text = "Flavor: " + this.cmboBoxJuiceFlav.Text;
else
msgBoxTxt += "Please select a flavor!";
if (msgBoxTxt != "")
MessageBox.Show(msgBoxTxt);