[英]How can I make this program check for multiple things one-at-a-time?
/ 此代碼的底部有兩個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!");
}
}
}
有更好的方法來做您想做的事情。 但是,為了解決您的問題,您可以嘗試以下方法-
.
.
.
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);
這不是您想要的嗎?
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!");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.