如何让这个程序一次一个地检查多个东西?

时间:2011-10-10 02:24:57

标签: c# if-statement

/ 在此代码的底部有两个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!");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

有更好的方法来做你想做的事情。但只是为了解决你的问题,你可以试试这个 -

.
.
.
   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);

答案 1 :(得分:0)

这不是你想要的:

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!");
}