gpt4 book ai didi

c# - 输入数字或 Q 退出

转载 作者:行者123 更新时间:2023-11-30 15:01:52 24 4
gpt4 key购买 nike

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

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

最多 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com