我怎样才能让这个程序一次检查多项内容?

发布于 2024-12-08 17:32:44 字数 3229 浏览 0 评论 0原文

/在此代码的底部有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你不是我要的菜∠ 2024-12-15 17:32:44

这不是你想要的吗:

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

Isn't this what you want:

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!");
}
旧竹 2024-12-15 17:32:44

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

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

There's better ways to do what you're trying to do. But just to get your problem solved, you could try this -

.
.
.
   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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文