我目前正在用C#编写Shop程序的代码.我对C#比较陌生,我很难让数学在下面的代码中工作:
//Add Basket
public void addBasket()
{
//Add up the total of individual items
double total = 0;
if (shoppingCart.Count() == 0)
{
Console.WriteLine("ERROR - Basket is Empty");
}
else
{
foreach (Products tmp in shoppingCart)
{
total = (tmp.Price * tmp.BoughtStock);
Console.WriteLine("The cost of the individual item is: " + "\t" +total);
}
}
//Calculate the products together
double itemTotal = 0;
if (shoppingCart.Count() == 0)
{
Console.WriteLine("ERROR - Basket is Empty");
}
else
{
foreach (Products tmp in shoppingCart)
{
itemTotal = (tmp.Price * tmp.BoughtStock);
itemTotal = itemTotal + total;
Console.WriteLine("The cost of the items together is: \t" +itemTotal);
}
//Calculate VAT
double vatPrice = total * .21;
double netPriceBeforeDiscount = total + vatPrice;
//calculate discount: if total cost of shop is over 25 give 10% discount.
if (netPriceBeforeDiscount >= 25)
{
double reducedPrice = netPriceBeforeDiscount * .10;
double netPrice = netPriceBeforeDiscount - reducedPrice;
reducedPrice = Math.Round(reducedPrice, 2);
netPrice = Math.Round(netPrice, 2);
Console.WriteLine("Discount*:\t\t\t\t " + reducedPrice);
Console.WriteLine("\nTotal Net Cost (including VAT and discounts):\t Euro " + netPrice);
}
else
{
double netPrice = Math.Round(netPriceBeforeDiscount, 2);
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码的第一部分正常工作,因为它在篮子中添加任何产品并单独显示价格,问题出现在第二部分,将篮子价格中的项目一起添加.正如您在输出中看到的那样 http://gyazo.com/1656eecc689b7a9d0bfc47b8480169a6(我必须链接输出的屏幕截图,因为我不知道如何在这里显示C#的输出)它显示第一项的总和,第二项,然后正确地将它们两个结果加在一起,尽管我不是我知道为什么它显示第二项的成本乘以2.最后,正如您可能在代码底部看到的那样,我已经写了我认为是获得增值税和显示批量折扣的正确方法,但是当我使用两个项目时代码将无法计算或显示增值税或批量折扣,但它将在篮子中有一个项目,请参见此处>(*链接号1下方到此处*).但是,从我的想象中,错误导致代码的其他部分无法正常工作,
正如我所说,虽然我是新手,并且在C#上并不是很好,但任何帮助都将非常感谢,如果你需要我的任何东西,请问,谢谢
编辑*:刚刚意识到我需要10个声望才能发布两个以上的链接,我在下面的评论中链接了2个缺失的链接.
foreach (Products tmp in shoppingCart)
{
total = (tmp.Price * tmp.BoughtStock);
Run Code Online (Sandbox Code Playgroud)
你可能意味着它total +=,否则你只保留最后一个值.