大家好,我一直有一些问题我已经设置输入数字或Q退出
任务任务的第一部分是产出价格表如下规则:
高达50的价格是每个5英镑。在51到80之间,价格是每个4英镑,而81到100之间的价格是每个2.50英镑。 使用循环结构和选择语句(如..等),你的程序应该输出为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但是当我需要使其根据订购的数量更改价格。
任何帮助或建议,非常感谢!
我不知道(或者)哟你的问题是,如果你有一个请澄清。但我认为你要求我们为你做你的功课..=) – CodingGorilla
如果你打算使用'int.TryParse',那么你不需要'try ... catch'块。 'TryParse'返回一个你应该测试的布尔值。如果它是假的,那么输入不能被解析。 –
问题是当我编译时,我的if语句决定了它们的成本项目数量,我得到的消息换行符不变。对不起,任何困惑= P但是,这是一个任务设置,而在uni,但是我的讲义没有提及任何关于终止程序=( – Skittle