好的,我从我能想到的各个角度都试过了,我想我像往常一样把事情复杂化了!
我正在构建一个控制台 c# 应用程序,该应用程序将要求用户输入所购买商品的价格,然后根据此客户代码为“客户代码”应用一定的折扣。
我为此使用了一个 switch 语句,并且它都适用于错误检查(使用 while 循环不断询问,直到识别出正确的输入)这是我正在努力的最后一部分......控制台询问用户是否愿意如果用户输入了不正确的输入,则使用我的代码输入更多数据(跳回主循环的开头),如果用户输入“N”,它也会根据需要再次询问程序终止。但是不起作用的一点是,如果用户输入'Y',他们应该能够回到开始并输入更多数据,但这不起作用=/我使用了“break;” 退出退出循环并返回主循环的语句...
此时字符是'Y',所以主循环应该仍在运行,但控制台什么都不做,它只是将光标放在一个空行上......它不要求输入,它没有说“按任意键继续”。我已经尽可能详细地介绍了这篇文章,我很抱歉=/ ...下面是我的代码...希望有人能发现我哪里出错了!
更新:我还应该注意,我已经尝试了使用 (=='Y') 的主循环并将变量设置为'Y',以便它执行第一个循环。我还用相同的语句将其更改为 do while 循环,以便首先使用空白字符运行,然后如果将其更改为“Y”,则应该排除循环条件 =/
更新:在你们都认为我更像个白痴之前,我已经注意到我的计算错误=/ LOL
namespace W7Task1
{
class W7Task1
{
// "Main" method begins the execution of the C# application
static void Main(string[] args)
{
char customerCode = '\0';
double initialCost = 0;
string customerType = "";
double finalPrice = 0;
string userInput = "";
char continueChar = '\0';
while (continueChar != 'N')
{
while (initialCost == 0)
{
Console.Write("\nPlease input the cost of the item: ");
userInput = Convert.ToString(Console.ReadLine());
try
{
initialCost = Convert.ToDouble(userInput);
}
catch
{
Console.WriteLine("\nPlease input a number!");
}
}
while (customerCode == '\0')
{
Console.Write("\nPlease input your customer code: ");
userInput = Convert.ToString(Console.ReadLine());
customerCode = Convert.ToChar(userInput);
customerCode = char.ToUpper(customerCode);
switch (customerCode)
{
case 'A':
customerType = "Managerial Staff";
finalPrice = (initialCost / 100) * 30 - initialCost;
Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
break;
case 'B':
customerType = "Sales Staff";
finalPrice = (initialCost / 100) * 20 - initialCost;
Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
break;
case 'C':
customerType = "Account Customers";
finalPrice = (initialCost / 100) * 8 - initialCost;
Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
break;
case 'D':
customerType = "Cash Customers";
finalPrice = (initialCost / 100) * 5 - initialCost;
Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
break;
case 'E':
customerType = "Credit Card/Cheque";
finalPrice = (initialCost / 100) * 0 - initialCost;
Console.WriteLine("\nThe initial cost of the item is: {0:c}\nYour customer type is: {1}\nThe items final price is: {2:c}\n", initialCost, customerType, finalPrice);
break;
default:
Console.WriteLine("\nError Please input a valid Customer Code\n");
customerCode = '\0';
break;
}
}
while (continueChar == '\0')
{
Console.WriteLine("Would you like to input more data?");
userInput = Convert.ToString(Console.ReadLine());
if (char.TryParse(userInput, out continueChar))
{
continueChar = char.ToUpper(continueChar);
if (continueChar == 'Y')
{
break;
}
else if (continueChar == 'N')
{
Console.WriteLine("Thankyou for using this application");
System.Environment.Exit(0);
}
else
{
Console.WriteLine("Please input a 'Y' or 'N'");
continueChar = '\0';
}
}
else
{
Console.WriteLine("Please input a valid character!");
}
}
}
}// End of "Main" method
}// End of "W7Task1" class
}