while循環不起作用。當它遇到休息以脫離循環時,我會收到一個錯誤。錯誤是沒有循環語句打破。使用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");
}
}
}
請描述你得到的錯誤。 – zneak 2010-09-15 03:21:50
@randy,編輯問題中的代碼並不真正有用(除非您提供更好的上下文)。您的編輯完全改變了問題的性質,這使得您收到的答案對其他與您遇到的問題類似問題的用戶無效。在Visual Studio中修復代碼,讓原始代碼保持原樣,以便將來的用戶可以找到解決方案。如果您遇到其他錯誤,請爲這些問題發佈單獨的問題。 – 2010-09-15 03:56:31
@randy,當你聲明'userin'時,給它一個初始值。 'string userin = null;'會工作得很好。這個新的錯誤是關於使用一個你還沒有初始化的變量(因爲你在'while'部分比較它而沒有分配一個原始值)。而且,請停止改變你的問題!發佈一個新的! – 2010-09-15 04:07:40