大家好,我在完成已设置的任务时遇到了一些问题
任务的第一部分是输出具有以下规则的价格表:
最多 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,但我需要让它根据订购的数量更改价格。
非常感谢任何帮助或建议!