【发布时间】:2010-09-15 03:19:44
【问题描述】:
while 循环不工作。当它遇到中断以退出循环时,我得到一个错误。错误是没有要跳出的循环语句。
static void Main(string[] args)
{
accounts myclass = new accounts();
string userin;
myclass.fillAccounts();
//int i = 0;
//while (i != 1)
do
{
}
while (userin != "x")
{
//use the following menu:
Console.WriteLine("*****************************************");
Console.WriteLine("enter an a or A to search account numbers");
Console.WriteLine("enter a b or B to average the accounts");
Console.WriteLine("enter an x or X to exit program");
Console.WriteLine("*****************************************");
Console.Write("Enter option-->");
userin = Console.ReadLine();
if (userin == "a" || userin == "A")
{
myclass.searchAccounts();
}
else if (userin == "b" || userin == "B")
{
myclass.averageAccounts();
}
else if (userin == "x" || userin == "X")
{
break;
}
else
{
Console.WriteLine("You entered an invalid option");
}
}
}
【问题讨论】:
-
请描述您遇到的错误。
-
@randy,编辑问题中的代码并没有真正的帮助(除非您提供更好的上下文)。您的编辑完全改变了问题的性质,这使得您收到的答案对于与您最初遇到的问题相似的其他用户无效。在 Visual Studio 中修复您的代码,让您的原始代码保持不变,以便将来的用户可以找到解决方案。如果您遇到其他错误,请针对这些问题发布单独的问题。
-
@randy,当你声明
userin时,给它一个初始值。string userin = null;可以正常工作。这个新错误与使用尚未初始化的变量有关(因为您在while部分比较它而没有为其分配原始值)。请停止改变你的问题!发布一个新的! -
现在我收到一个新错误 错误 1 一个名为 'userin' 的局部变量已在此范围内定义
-
对于如此简单的事情似乎如此愚蠢地困难
标签: c# loops while-loop