goto 语句在此示例中如何工作?
我正在研究这个代码示例:
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();
}
}
我不明白为什么调用“Not Found”语句,如果我输入像 10 这样的搜索编号,在控制台上打印相应的消息,在这种情况下 goto: Found 语句正在执行,所以 goto: NotFound语句永远不会被调用,但其相应的消息仍然打印在控制台上,我不明白如何,因为在这种情况下程序永远不会跳转到这个“NotFound”标签。
请您现在帮我解决这个问题...
谢谢
I am studying this code sample:
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();
}
}
I do not understand why the "Not Found" statement was called and its corresponding message print on console if i enter a seek number like 10, in this case goto: Found statement is executing, so goto: NotFound statement will never be called, but still its corresponding message is printed on console, i do not understand how since in this case program never jumps to this "NotFound" label.
Please if you now give me a hand about this...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Eww goto 的,我会使用
if/else
语句,但如果您需要 goto 的:Eww goto's, I would use and
if/else
statement, but if you need goto's:我会重写这段代码以避免使用 goto:
I would rewrite this code to avoid the use of goto:
之所以调用它,是因为 Found 标签内的代码没有任何东西可以强制它跳过 NotFound 标签内的代码(除非您再次调用 goto,否则执行将通过标签而不是跳过它)。
话虽这么说,不要使用
goto
!我会尽可能地说,这总是是一个坏主意,可以重新 -书面。对于您的情况,您可以添加一个简单的布尔标志来摆脱您的转到:
It's being called because your code inside of the Found label has nothing to force it to skip over the code int he NotFound label (unless you call goto again, the execution will fall through the label rather than skip it).
That being said, don't use
goto
! I'll go as far to say that it's always a bad idea and can be re-written.In your case, you can add a simple boolean flag to get rid of your goto:
因为您只是跳转到“已找到”标签,然后继续跳转到“未找到”标签。您需要第三个名为 EndFound 的标签,并在找到后转到它。
Because you just jump to the Found label and proceed to fall through to the Not Found label. You'd need a third label called EndFound and go to it after found.
如果您不想在执行“Found”语句时执行“Not Found”语句,请使用另一个 goto 来跳过 NotFound 部分。 goto 跳转到一个节,但这并不意味着如果不通过 goto 跳转到该节,该节就不会被执行。请记住,代码以自上而下的方式执行,因此除非您以某种方式跳过一段代码,否则它将执行。
例子:
if you don't want the "Not Found" statments to execute if the "Found" statments execute, use another goto, to skip the NotFound portion. goto jumpts to a section, but that doesn't mean the section won't be executed if it is not jumped to via a goto. Remember, the code executes in a top down fasion, so unless you somehow skip over a peice of code, it will execute.
example:
因为在跳转到 Found 后,执行会继续到下一行,这恰好是“未找到”控制台写入行。您需要添加另一个 goto 来跳过它(或者更好的是,重新设计它以完全避免 goto)
正是这样的问题,应该避免 goto。
Because after it jump to
Found
, execution just continues to the next line, which happens to be the "not found" console write line. You need to add yet another goto to jump over that (or better yet, redesign this to avoid the gotos entirely)It is exactly problem like this, that gotos should be avoided.