c# - 输入数字或 Q 退出

标签 c#

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

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

最多 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,但这是我需要让它根据订购数量更改价格的时候。

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

最佳答案

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

修复这些问题,一切正常。

关于c# - 输入数字或 Q 退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13457411/

相关文章:

c# - 尝试序列化 PropertyInfo 时引发异常

c# - 换行的电子邮件中的双倍小数点

c# - 如何将 List 的多个值放入 M​​essageBox

c# - 如何从一个线程使用 Console.ReadLine 并从另一个线程使用 ConsoleKeys?

c# - 如何设置 WPF 应用程序的默认字体?

c# - c# 中 ReDimStatement 的等价物是什么?

c# - ORA-00922 : missing or invalid option When trying to execute PL/SQL from C#

c# - 即使我添加了AllowLoadLocalInfile,该MySQL版本也不允许使用该命令

c# - Monodroid BitmapFactory.DecodeFileDescriptor 位图始终为 null

C# - 图表光标不跟随鼠标点值