因此,正如标题所示,我试图在C#中使用Do-While语句来制作收据。我希望发生的是,当下订单时,Do-While语句将能够看到所购买的商品以及所购商品的总数。
所以,例如,我有两个售价2.20英镑的浓咖啡,一个售价2.75英镑的热巧克力和一个售价2.75英镑的摩卡咖啡。
我希望收据看起来像:
Espresso x2 - 4.40英镑 热巧克力x1 - £2.75 摩卡x1 - £2.75
当然,如果说订单有两个摩卡而不是一个,也许是一个额外的项目,比如三个美式咖啡,价格为2.50英镑,它看起来像:
Espresso x2 - 4.40英镑 热巧克力x1 - £2.75 摩卡x2 - 5.50英镑 Americano x3 - £7.50
我理解如何做一个简单的Do-While代码,但是我正在尝试的东西(如果它可以完成)已经卡住了,我已经苦苦挣扎了一段时间。
任何帮助将不胜感激。
我的代码目前看起来像这样,以使计算有效:
namespace While_Receipt
{
public partial class Form1 : Form
{
decimal priceespresso, totalespresso, pricefilter, totalfilter, priceCappuccino, totalCappuccino, priceAmericano,
totalAmericano, priceLatte, totalLatte, priceMocha, totalMocha, priceHotChocolate, totalHotChocolate,
priceIceCoffee, totalIceCoffee, priceExtra, totalExtra, TotalOverall;
int quantityespresso, quantityfilter, quantitiyCappuccino, quantityAmericano, quantityLatte, quantitiyMocha,
quantitiyHotChocolate, quantityIceCoffee, quantityExtra;
public Form1()
{
InitializeComponent();
}
private void btnTotal_Click(object sender, EventArgs e)
{
priceespresso = Convert.ToDecimal(txtEspressoPrice.Text);
quantityespresso = Convert.ToInt32(txtEspressoMultiply.Text);
totalespresso = priceespresso * quantityespresso;
lblResults.Text = Convert.ToString(totalespresso);
pricefilter = Convert.ToDecimal(txtFilterPrice.Text);
quantityfilter = Convert.ToInt32(txtFilterMultiply.Text);
totalfilter = pricefilter * quantityfilter;
lblFilterResults.Text = Convert.ToString(totalfilter);
priceCappuccino = Convert.ToDecimal(txtCappuccinoPrice.Text);
quantitiyCappuccino = Convert.ToInt32(txtAmericanoMultiply.Text);
totalCappuccino = priceCappuccino * quantitiyCappuccino;
lblCappuccinoResults.Text = Convert.ToString(totalCappuccino);
priceAmericano = Convert.ToDecimal(txtAmericanoPrice.Text);
quantityAmericano = Convert.ToInt32(txtCappuccinoMultiply.Text);
totalAmericano = priceAmericano * quantityAmericano;
lblAmericanoResults.Text = Convert.ToString(totalAmericano);
priceLatte = Convert.ToDecimal(txtLattePrice.Text);
quantityLatte = Convert.ToInt32(txtLatteMultiply.Text);
totalLatte = priceLatte * quantityLatte;
lblLatteResults.Text = Convert.ToString(totalLatte);
priceMocha = Convert.ToDecimal(txtMochaPrice.Text);
quantitiyMocha = Convert.ToInt32(txtMochaMultiply.Text);
totalMocha = priceMocha * quantitiyMocha;
lblMochaResults.Text = Convert.ToString(totalMocha);
priceHotChocolate = Convert.ToDecimal(txtHotChocolatePrice.Text);
quantitiyHotChocolate = Convert.ToInt32(txtHotChocolateMultiply.Text);
totalHotChocolate = priceHotChocolate * quantitiyHotChocolate;
lblHotChocolateResults.Text = Convert.ToString(totalHotChocolate);
priceIceCoffee = Convert.ToDecimal(txtIceCoffeePrice.Text);
quantityIceCoffee = Convert.ToInt32(txtIcedCoffeeMultiply.Text);
totalIceCoffee = priceIceCoffee * quantityIceCoffee;
lblIcedCoffeeResults.Text = Convert.ToString(totalIceCoffee);
priceExtra = Convert.ToDecimal(txtExtra.Text);
quantityExtra = Convert.ToInt32(txtExtraMultiply.Text);
totalExtra = priceExtra * quantityExtra;
txtExtraResults.Text = Convert.ToString(totalExtra);
TotalOverall = totalespresso + totalfilter + totalCappuccino + totalAmericano + totalLatte
+ totalMocha + totalHotChocolate + totalIceCoffee + totalExtra;
lblTotalOverall.Text = Convert.ToString(TotalOverall);
}
private void lstResults_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:1)
在这种情况下,你可以用do-while循环做很多事情。您可以使用if
逐个检查每个数量,以查看它们是否大于0,以及何时将它们输出到收据上。
我会向你推荐一些学习内容(来自你找到的C#书)。首先要了解课程,为收据明细创建一个课程,其中包含所售产品的属性,数量,价格和价值。
然后,您应该了解集合,将正在销售的产品放入集合中。
您将能够在网格控件或ListView中显示集合的内容。
完成这些步骤后,您将能够以一个周期(而不是具有foreach
周期的执行周期)浏览集合的内容,并将这些项目输出到收据。