goto 语句在此示例中如何工作?

发布于 2024-09-16 13:27:12 字数 1161 浏览 4 评论 0原文

我正在研究这个代码示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

幸福丶如此 2024-09-23 13:27:12

Eww goto 的,我会使用 if/else 语句,但如果您需要 goto 的:

Found: 
  Console.WriteLine("Numarul a fost gasit");
  goto End;
NotFound:
  Console.WriteLine("Numarul nu a fost gasit !");
End:
Console.ReadKey();

Eww goto's, I would use and if/else statement, but if you need goto's:

Found: 
  Console.WriteLine("Numarul a fost gasit");
  goto End;
NotFound:
  Console.WriteLine("Numarul nu a fost gasit !");
End:
Console.ReadKey();
赢得她心 2024-09-23 13:27:12

我会重写这段代码以避免使用 goto:

string message;
if (myArrayTable.Cast<string>().Contains(cautat)) {
    message = "Found";
} else {
    message = "Not found!";
}
Console.WriteLine(message);

I would rewrite this code to avoid the use of goto:

string message;
if (myArrayTable.Cast<string>().Contains(cautat)) {
    message = "Found";
} else {
    message = "Not found!";
}
Console.WriteLine(message);
烟若柳尘 2024-09-23 13:27:12

之所以调用它,是因为 Found 标签内的代码没有任何东西可以强制它跳过 NotFound 标签内的代码(除非您再次调用 goto,否则执行将通过标签而不是跳过它)。

话虽这么说,不要使用goto我会尽可能地说,这总是是一个坏主意,可以重新 -书面。

对于您的情况,您可以添加一个简单的布尔标志来摆脱您的转到:

static void Main(string[] args)
{
    int x = 10, y = 10;
    bool isFound = false;

    // Rest of the body

    for(int i=0;i<x;i++)
    {
        for(int j=0;j<y;j++)
        {
            if(cautat.Equals(myArrayTable[i,j]))
            {
                isFound = true;
                break;
            }
        }

        if(isFound)
            break;
    }

    if(isFound)
        Console.WriteLine("Numarul a fost gasit");
    else
        Console.WriteLine("Numarul nu a fost gasit!");

    Console.ReadKey();
}

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:

static void Main(string[] args)
{
    int x = 10, y = 10;
    bool isFound = false;

    // Rest of the body

    for(int i=0;i<x;i++)
    {
        for(int j=0;j<y;j++)
        {
            if(cautat.Equals(myArrayTable[i,j]))
            {
                isFound = true;
                break;
            }
        }

        if(isFound)
            break;
    }

    if(isFound)
        Console.WriteLine("Numarul a fost gasit");
    else
        Console.WriteLine("Numarul nu a fost gasit!");

    Console.ReadKey();
}
此生挚爱伱 2024-09-23 13:27:12

因为您只是跳转到“已找到”标签,然后继续跳转到“未找到”标签。您需要第三个名为 EndFound 的标签,并在找到后转到它。

Found: 
    Console.WriteLine("Numarul a fost gasit");
    goto EndFound;
NotFound:
    Console.WriteLine("Numarul nu a fost gasit !");
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: 
    Console.WriteLine("Numarul a fost gasit");
    goto EndFound;
NotFound:
    Console.WriteLine("Numarul nu a fost gasit !");
EndFound:
温柔戏命师 2024-09-23 13:27:12

如果您不想在执行“Found”语句时执行“Not Found”语句,请使用另一个 goto 来跳过 NotFound 部分。 goto 跳转到一个节,但这并不意味着如果不通过 goto 跳转到该节,该节就不会被执行。请记住,代码以自上而下的方式执行,因此除非您以某种方式跳过一段代码,否则它将执行。

例子:

Found:  
    Console.WriteLine("Numarul a fost gasit"); 

goto ReadKey;

NotFound: 
    Console.WriteLine("Numarul nu a fost gasit !"); 

ReadKey:
    Console.ReadKey(); 

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:  
    Console.WriteLine("Numarul a fost gasit"); 

goto ReadKey;

NotFound: 
    Console.WriteLine("Numarul nu a fost gasit !"); 

ReadKey:
    Console.ReadKey(); 
開玄 2024-09-23 13:27:12

因为在跳转到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文