c# – 为什么我的While循环不循环?
作者:互联网
好吧,我从我能想到的每一个角度尝试过这个,我认为我按照惯例过度复杂化了!
我正在构建一个控制台c#应用程序,它将要求用户输入所购买商品的价格,然后根据此客户代码输入“客户代码”,将应用某个折扣.
我为此使用了一个switch语句,它都与错误检查一起工作(使用while循环继续询问,直到识别出正确的输入)这是最后一部分我正在努力…控制台询问用户他们是否愿意如果用户输入了错误的输入,则根据需要再输入更多数据(跳回主循环的开头),如果用户输入’N’,程序也会终止.但是不起作用的是如果用户输入“Y”,他们应该能够返回到开始并输入更多数据,但这不起作用= /我使用了“中断”;声明打破退出循环并返回主循环…
此时字符为’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
}
解决方法:
发生的事情是,在用户输入’Y’之后,外循环(即while(continueChar!=’N’))将继续无限循环,但没有内循环(从while开始(initialCost == 0) ))将满足它们的条件,因为它们的变量(例如initialCost)将保留在前一次迭代中分配的值.
最简单的解决方法是将所有变量初始化移动到外部循环中.更改您的代码:
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)
{
// ...
…至:
static void Main(string[] args)
{
char continueChar = '\0';
while (continueChar != 'N')
{
char customerCode = '\0';
double initialCost = 0;
string customerType = "";
double finalPrice = 0;
string userInput = "";
while (initialCost == 0)
{
// ...
编辑:如果要在方法的顶部保留变量声明,可以将它们的声明和初始化分开.这将确保它们在内循环的每次迭代中重置,同时保持您的导师满意.
static void Main(string[] args)
{
char customerCode;
double initialCost;
string customerType;
double finalPrice;
string userInput;
char continueChar = '\0';
while (continueChar != 'N')
{
customerCode = '\0';
initialCost = 0;
customerType = "";
finalPrice = 0;
userInput = "";
while (initialCost == 0)
{
// ...
标签:c,console-application,while-loop,break,nested-loops 来源: https://codeday.me/bug/20190529/1180756.html