简体   繁体   English

C#中的数学错误

[英]Math errors in C#

I am currently writing the code for a Shop program in C#. 我目前正在用C#编写Shop程序的代码。 I am relatively new to C# and I am having difficulty in getting the math to work in the following piece of code: 我对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);
      }
   }
}

The first part of the code works correctly, as in that it adds any products in the basket and displays there prices individually, the problem arises in the second part, adding the items in the baskets prices together. 代码的第一部分正常工作,因为它在篮子中添加任何产品并单独显示价格,问题出现在第二部分,将篮子价格中的项目一起添加。 As you can see in the output http://gyazo.com/1656eecc689b7a9d0bfc47b8480169a6 (I have to link a screencap of the output as I don't know how to show output from C# on here) It displays the total of the first item, the second item and then correctly adds them two results together, although I don't know why it displays the cost of the second item multiplied by two. 正如你在输出http://gyazo.com/1656eecc689b7a9d0bfc47b8480169a6中看到的那样(我必须链接输出的screencap,因为我不知道如何在这里显示C#的输出)它显示第一项的总和,第二个项目,然后正确地将它们两个结果加在一起,虽然我不知道为什么它显示第二个项目的成本乘以2。 Finally as you may see at the bottom of the code, I have written what i believe to be a correct way of getting the VAT and displaying a bulk discount but from the link above when I use two items the code will not calculate or display the VAT or bulk discount yet with one item in the basket it will, see here > ( * Link Number 1 Below goes here *) . 最后,正如您可能在代码底部看到的那样,我已经写了我认为是获得增值税和显示批量折扣的正确方法,但是当我使用两个项目时代码将无法计算或显示增值税或批量折扣,但它将在篮子中有一个项目,请参见此处>(*链接号1下方到此处*)。 Again though ,from what I imagine is the error causing the other parts of the code to not work properly, when I do just one item despite calculating the vat and bulk discounts correctly and displaying the correct answer it multiplys the individual item cost by the amount of the product I bought, see here > ( * Link Number 2 Below goes here *) 尽管如此,从我想象的是导致代码的其他部分无法正常工作的错误,当我只做一个项目尽管正确计算增值税和批量折扣并显示正确答案时,它会将单个项目成本乘以金额我买的产品,请看这里>(*链接号2下面到这里*)

As I said though I am new to this and well not exactly great at C# but any help would be greatly appreciated and if you require anything from me just ask, Thanks 正如我所说,虽然我是新手,并且在C#上并不是很好,但任何帮助都将非常感谢,如果你需要我的任何东西,请问,谢谢

Edit* : Just realised I need 10 reputation to post more than two links, I link the 2 missing links in the comments below. 编辑*:刚刚意识到我需要10个声望才能发布两个以上的链接,我在下面的评论中链接了2个缺失的链接。

foreach (Products tmp in shoppingCart)
        {
            total = (tmp.Price * tmp.BoughtStock);

You probably mean for it to be total += , otherwise you are only keeping the last value. 你可能意味着它是total += ,否则你只保留最后一个值。

Your second loop: 你的第二个循环:

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

Is also very odd. 也很奇怪。 You are again overwriting itemTotal each time round the loop, but then just adding the previously calculated total to this result. 您每次循环时都会覆盖itemTotal ,但之后只需将先前计算的总数添加到此结果中。

I don't know what you are intending here so I hesitate to suggest that you just need to use += again - but it's definitely wrong. 我不知道你打算在这里做什么,所以我犹豫建议你只需要再次使用+= - 但这绝对是错误的。

However your Console.WriteLine statement appear to suggest that you want to display the price of each line in the transaction. 但是,您的Console.WriteLine语句似乎表明您要显示事务中每一行的价格。 In that case you'll need to do something like this: 在这种情况下,你需要做这样的事情:

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

Note that I'm using decimal as this gives more consistent results when dealing with money. 请注意,我使用decimal因为这可以在处理金钱时提供更一致的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM