C# C语言中的数学错误#

C# C语言中的数学错误#,c#,math,C#,Math,我目前正在用C#为一个商店程序编写代码。我对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

我目前正在用C#为一个商店程序编写代码。我对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);
      }
   }
}
代码的第一部分工作正常,因为它在篮子中添加任何产品并单独显示价格,问题出现在第二部分,将篮子中的项目添加到一起。正如您在输出中所看到的(我必须链接输出的屏幕盖,因为我不知道如何在这里显示C#上的输出),它显示第一项和第二项的总计,然后将它们正确地相加,两个结果,尽管我不知道为什么它显示第二项的成本乘以2。最后,正如您在代码底部看到的,我已经编写了我认为是获取增值税和显示批量折扣的正确方法,但从上面的链接,当我使用两个项目时,代码将不会计算或显示增值税或批量折扣,但篮子中有一个项目,请参见此处>(*下面的链接编号1在此处*).不过,从我的想象来看,错误导致代码的其他部分无法正常工作,当我只做一项时,尽管正确计算了增值税和批量折扣并显示了正确答案,但它会将单个项目的成本乘以我购买的产品的金额,请参见此处>(*下面的链接2位于此处*)

正如我所说,虽然我对这一点还不熟悉,而且在C#方面也不太在行,但如果您需要任何帮助,我们将不胜感激。如果您需要我的帮助,请尽管问,谢谢

编辑*:刚刚意识到我需要10个声誉来发布两个以上的链接,我在下面的评论中链接了两个缺失的链接

foreach (Products tmp in shoppingCart)
        {
            total = (tmp.Price * tmp.BoughtStock);
您可能希望它是
total+=
,否则您只保留最后一个值。

您的第二个循环:

        foreach (Products tmp in shoppingCart)
        {
            itemTotal = (tmp.Price * tmp.BoughtStock);
            itemTotal = itemTotal + total;
            Console.WriteLine("The cost of the items together is: \t" +itemTotal);
        }
这也很奇怪。每次循环时,您都会再次覆盖
itemTotal
,但只需将之前计算的总数添加到此结果中即可

我不知道您在这里的意图是什么,所以我不太愿意建议您只需要再次使用
+=
,但这绝对是错误的

但是,您的
Console.WriteLine
语句似乎建议您显示交易中每一行的价格。在这种情况下,您需要执行以下操作:

decimal transactionTotal = 0;
foreach (Products tmp in shoppingCart)
{
    decimal lineTotal = (tmp.Price * tmp.BoughtStock);
    transactionTotal += lineTotal;
    Console.WriteLine("The cost of the items together is: \t" + lineTotal);
}

请注意,我使用的是
decimal
,因为这样在处理货币时会得到更一致的结果。

链接编号1>链接编号2>除了其他内容之外-不要对货币值使用
double
-使用
decimal
。您能再添加一些
控制台吗?WriteLine
s?找出C#的数学在哪里给了你错误的东西,然后从那里开始。好吧,为我糟糕的代码和描述感到抱歉,我想在这里做的是将我第一个循环中所有单个总计的价格相加,这样我就可以得到客户的总成本<希望这能解释我想做得更好一点的原因