【发布时间】:2010-08-31 16:46:16
【问题描述】:
我正在研究这个代码示例:
class Program
{
static void Main(string[] args)
{
int x = 10;
int y = 10;
int generate=0;
string [,] myArrayTable = new string[x, y];
Console.WriteLine("Enter a seek number: ");
string cautat = Console.ReadLine();
for (int i = 0; i < x; i++)
{
for(int j = 0;j < y; j++)
{
myArrayTable[i, j] = (generate++).ToString();
}
}
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
if(cautat.Equals(myArrayTable[i,j]))
{
goto Found;
}
}
}
goto NotFound;
Found:
Console.WriteLine("Numarul a fost gasit");
NotFound:
Console.WriteLine("Numarul nu a fost gasit !");
Console.ReadKey();
}
}
如果我输入像 10 这样的搜索编号,我不明白为什么会调用“未找到”语句并且在控制台上打印相应的消息,在这种情况下 goto: Found 语句正在执行,所以 goto: NotFound 语句将永远不会调用,但其相应的消息仍然打印在控制台上,我不明白为什么在这种情况下程序永远不会跳转到这个“NotFound”标签。
如果你现在请帮我解决这个问题...
谢谢
【问题讨论】:
-
goto误用的好例子 -
你真的应该重组它,不要使用 goto 。这通常不被认为是好的做法,所以你会在代码审查中得到很多摇头,而且它往往是不可维护的。坦率地说,这会散发出明显的代码气味。