C# 为什么赢了';我的While循环,循环?
好吧,我从我能想到的每个角度都试过了,我觉得我像往常一样把事情复杂化了 我正在构建一个控制台c#应用程序,它将要求用户输入购买物品的价格,然后根据该客户代码为“客户代码”应用一定的折扣 我使用了一个switch语句,它与错误检查一起工作(使用while循环不断询问,直到识别出正确的输入),这是我正在努力的最后一部分。。。如果用户输入错误的输入,控制台会询问用户是否希望使用我的代码输入更多数据(跳回到主循环的开始处)。如果用户输入“N”,程序也会根据需要再次询问。但不起作用的一点是,如果用户输入“Y”,他们应该能够返回到开始并输入更多数据,但这不起作用=/I使用“break;”语句中断退出循环并返回主循环 此时字符为“Y”,因此主循环应仍在运行,但控制台不执行任何操作,只是将光标放在空行上。。。它不要求输入,也不说“按任意键继续”。我已经尽可能详细地讲了,我为这篇文章感到抱歉。。。下面是我的代码。。。希望有人能发现我哪里出了问题 更新:我还应该注意,我已经用(='Y')尝试了主循环,并将变量设置为'Y',以便它执行第一个循环。我还使用相同的语句将其更改为do while循环,以便首先使用空白字符运行,然后如果将其更改为“Y”,则循环条件应被排除在外=/ 更新:在你们认为我更像个白痴之前,我已经注意到了我的计算错误=/LOLC# 为什么赢了';我的While循环,循环?,c#,while-loop,console-application,break,nested-loops,C#,While Loop,Console Application,Break,Nested Loops,好吧,我从我能想到的每个角度都试过了,我觉得我像往常一样把事情复杂化了 我正在构建一个控制台c#应用程序,它将要求用户输入购买物品的价格,然后根据该客户代码为“客户代码”应用一定的折扣 我使用了一个switch语句,它与错误检查一起工作(使用while循环不断询问,直到识别出正确的输入),这是我正在努力的最后一部分。。。如果用户输入错误的输入,控制台会询问用户是否希望使用我的代码输入更多数据(跳回到主循环的开始处)。如果用户输入“N”,程序也会根据需要再次询问。但不起作用的一点是,如果用户输入“
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
}您没有重置变量 当用户退出退出循环时,变量如下
continueChar == 'Y'
customerCode != '\0'
initialCost != 0
这意味着所有while循环都不会触发
将变量声明或初始化移动到主循环内。发生的情况是,在用户输入
'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)
{
// ...
您是否已使用调试器(在Visual studio中按“F10”)逐步完成应用程序
您没有将'initialCost'变量重置为0。我知道这非常简单!我只是有一个“哦,是的……嗯!”的时刻。非常感谢你,我第一眼也看不见了。但当我通过代码时,它变得很明显。尝试学习如何使用VisualStudio调试器;在这种情况下,它非常有帮助。我是一名一年级学生,他们只告诉我们“在不调试的情况下运行”=/就我个人而言,我看到它们之间的唯一区别是,当不再执行代码时,调试将关闭控制台,而不进行调试将保持窗口打开,要求您手动退出。我们还被要求在方法顶部声明所有变量,否则我将错过演示标记=/我将不得不询问我的导师如果明天我会因为稍微向下移动而失去分数,因为我并没有将它们全部拆分,这有助于功能。如果在代码中设置断点,您将看到这两种模式之间的差异。我的代码中的最后一次编辑应该满足你导师的要求。我会复制并粘贴我对上述答案的评论,但这将是粗鲁的,因此也感谢你的回答=]我也是,因为我也在做“黑盒测试”(我对此感到困惑,所以我刚刚列出了每次测试中发生的所有事情,当我理解时,我可以将其整理成黑盒格式。谢谢您的回复