3

大家好,我在完成已设置的任务时遇到了一些问题

任务的第一部分是输出具有以下规则的价格表:

最多 50 个的价格为每个 5 英镑。51 到 80 之间的价格为 4 英镑,而 81 到 100 之间的价格最低为 2.50 英镑。使用循环结构和选择语句(if.. 等),您的程序应输出 10 到 100 的倍数的小部件价格图表。

我已经这样做了,但是在输出表格以输入许多小部件后,任务的第二部分让我很难过。然后,您应该计算成本并输出价值。如果用户输入“q”或“Q”,程序应该终止。

这是完整的代码

using System;
namespace w5Task3
{
class Program
    { public static void Main ( string[] args )
            {

            double PriceEach1 = 5;
            double PriceEach2 = 4;
            double PriceEach3 = 2.50;
            double Quantity = 10;
            int UserOrder=0;

            Console.WriteLine("\n\nBelow is the price chart:\n\n");
            Console.WriteLine("WidgetQuantity\t\t\tPrice\n");
            while (Quantity <=100)

                {   

                    double Price1 = PriceEach1*Quantity;
                    double Price2 = PriceEach2*Quantity;
                    double Price3 = PriceEach3*Quantity;


                        if (Quantity <=50)      
                    {
                        Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price1);
                    }


                        if(Quantity >=51 && Quantity <=80)      
                    {
                        Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price2);
                    }


                        if (Quantity >80 && Quantity <=100)
                    {
                        Console.WriteLine ("\t{0}\t\t\t{1:C}",Quantity, Price3);                        
                    }
                Quantity +=10;
                }


            while (UserOrder >=0)
                {
                    try
                        {
                        Console.WriteLine("Enter the amount of widgets you would like to purchase or press q to quit");
                        string temp = Console.ReadLine();
                        if (temp =="q") break;
                        if (temp =="Q") break;

                        int.TryParse(temp, out UserOrder);

                        double UserPrice;

                        if (UserOrder <=50)
                            {
                                UserPrice = UserOrder*5;
                                Console.WriteLine("The price is {0:C}",UserPrice);
                            }

                        if (UserOrder >=51 && UserOrder <=80)
                            {
                                UserPrice = UserOrder*4;
                                Console.WriteLine("The price is {0:C}",UserPrice");
                            }

                        if (UserOrder >80)
                            {
                                UserPrice = UserOrder*2.5;
                                Console.WriteLine("The price is {0:C}",UserPrice");
                            }


                        }
                            catch(Exception)
                                {
                                    Console.WriteLine("You have entered an incorrect value. Please enter a number or press q to quit");
                                }
                }



            }
    }

我遇到问题的部分是:

                while (UserOrder >=0)
                {
                    try
                        {
                        Console.WriteLine("Enter the amount of widgets you would like to purchase or press q to quit");
                        string temp = Console.ReadLine();
                        if (temp =="q") break;
                        if (temp =="Q") break;

                        int.TryParse(temp, out UserOrder);

                        double UserPrice;

                        if (UserOrder <=50)
                            {
                                UserPrice = UserOrder*5;
                                Console.WriteLine("The price is {0:C}",UserPrice);
                            }

                        if (UserOrder >=51 && UserOrder <=80)
                            {
                                UserPrice = UserOrder*4;
                                Console.WriteLine("The price is {0:C}",UserPrice");
                            }

                        if (UserOrder >80)
                            {
                                UserPrice = UserOrder*2.5;
                                Console.WriteLine("The price is {0:C}",UserPrice");
                            }


                        }
                            catch(Exception)
                                {
                                    Console.WriteLine("You have entered an incorrect value. Please enter a number or press q to quit");
                                }
                }
}

我可以让程序退出或执行一个 UserPrice,但我需要让它根据订购的数量更改价格。

非常感谢任何帮助或建议!

4

4 回答 4

2

第 67 行和第 73 行的常量表达式中有换行符!

修复这些,一切运行良好。

于 2012-11-19T16:04:47.580 回答
2

我认为您的问题是第 64 行和第 70 行 UserPrice 之后的报价。

Console.WriteLine("The price is {0:C}",UserPrice");

去掉它

于 2012-11-19T16:09:26.943 回答
1

最简单的方法是UserOrder在计算该部分订单的价格后进行调整。你可以朝任何一个方向走,但它基本上看起来像:

if (HasThisQuantityRange)
{
   Total += ThisAmount * (Lower of: ThisQuantity or UserOrder);
   UserOrder -= (Lower of: ThisQuantity or UserOrder);
}

if (UserOrder > 0 && HasAdjustedQuantityRange)
{
   Total += ThisAmount * (Lower of: ThisQuantity or UserOrder);
   UserOrder -= (Lower of: ThisQuantity or UserOrder);
}

等等。

显然,这都是伪代码,您需要自己实现它,但这有望让您找到正确的方向。

于 2012-11-19T16:01:53.073 回答
0

我无法理解您遇到的问题到底是什么。您是在努力为订单进行正确的计算,还是 Quit 行为没有按照您期望的方式运行。

一般来说,我认为在你的 while 循环条件中使用像这样的瞬态变量是不好的形式。当我希望控制台应用程序重复时,我通常会设置一个专门用于循环条件的变量,如下所示:

bool isRunning = true;
while (isRunning)
{
     ....

然后当你检查你的输入

if (temp.ToUpper() == "Q")
{
     isRunning = false;
     break;
}
于 2012-11-19T16:07:22.403 回答