代码之家  ›  专栏  ›  技术社区  ›  Beth Morgan

C#如何将循环中的字符串值作为列表项返回

  •  0
  • Beth Morgan  · 技术社区  · 2 年前

    我有一个披萨店订单系统的应用程序,它要求顾客输入他们想要订购的披萨数量,并根据数量选择他们的披萨底料和浇头选项。我希望能够将每个披萨的外皮和浇头值作为字符串列在程序底部的发票部分。

    有人知道我如何将每个披萨的字符串值传递到底部的发票部分吗?

    // Ask user for pizza order details
                //crust selection
                qtyPizzas = numPizzas();
                for (loop = 1; loop <= qtyPizzas; loop++)
                {
                    crustOption = selectCrust();
                    crustPrice = crustCost(crustOption);
                    if (crustOption == 1)
                    {
                        crustString = "thin crust";
                    }
                    else
                    {
                        crustString = "deep pan";
                    }
                    //topping selection
                    toppingOption = selectTopping();
                    toppingPrice = toppingCost(toppingOption);
                    if (toppingOption == 1)
                    {
                        toppingString = "ham";
                    }
                    else if (toppingOption == 2)
                    {
                        toppingString = "mushrooms";
                    }
                    else if (toppingOption == 3)
                    {
                        toppingString = "mediterranean vegetables";
                    }
                    else if (toppingOption == 4)
                    {
                        toppingString = "extra cheese";
                    }
                    unitPrice = crustPrice + toppingPrice;
                    totalPizzaCost = totalPizzaCost + unitPrice;
                    Console.WriteLine("- Pizza #" + loop + ": " + crustString + " with " + toppingString + " (£" + unitPrice + ")");
                    pizzaSelection = Convert.ToString(crustString + " " + toppingString);
                }
    
                Console.Write("pizza delivery? (y/n): ");
                deliveryOption = Console.ReadLine();
    
                // Calculate delivery cost based on distance
                if (deliveryOption == "y")
                {
                    Console.Write("Enter delivery distance in miles: ");
                    deliveryDistance = Convert.ToDouble(Console.ReadLine());
    
                    if (deliveryDistance <= 2)
                    {
                        deliveryCost += 0;
                    }
                    else if (deliveryDistance <= 5)
                    {
                        deliveryCost += 3.00;
                    }
                    else if (deliveryDistance <= 8)
                    {
                        deliveryCost += 5.00;
                    }
                    else
                    {
                        Console.WriteLine("Sorry, we do not deliver that far.");
                    }
                }
    
    
                // calculate total cost
                vat = (totalPizzaCost + deliveryCost) * (100 + 20) / 100;
                orderVAT = vat - totalPizzaCost - deliveryCost;
    
                // Output the invoice
                Console.WriteLine("");
                Console.WriteLine("INVOICE");
                Console.WriteLine("------------------------------");
                Console.WriteLine("Customer ID: " + customerID);
                Console.WriteLine("");
                Console.WriteLine("Pizzas:");
    
                for (int i = 1; i <= qtyPizzas; i++)
                {
                    Console.WriteLine("pizza # " + i + ": " + pizzaSelection);
                }
                Console.WriteLine("  Total pizza costs: " + totalPizzaCost.ToString("C"));
                Console.WriteLine("  Delivery cost: " + deliveryCost.ToString("C"));
                Console.WriteLine("  VAT: " + orderVAT.ToString("C"));
                Console.WriteLine("  Grand total (incl. VAT): " + vat.ToString("C"));
                Console.WriteLine("------------------------------");
                Console.ReadKey();
    

    我尝试在发票部分使用for循环来执行此操作,它会显示客户指定要订购的披萨的正确数量,但它只显示每个字符串中的最后一个饼皮和浇头值。

    0 回复  |  直到 2 年前