我正在研究这个代码示例:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么调用"Not Found"语句并在控制台上打印相应的消息,如果我输入一个10的搜索号,在这种情况下goto:Found语句正在执行,所以goto:NotFound语句永远不会被调用,但是仍然在控制台上打印相应的消息,我不明白,因为在这种情况下程序永远不会跳转到这个"NotFound"标签.
如果你现在帮我解决这个问题......
谢谢
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();
Run Code Online (Sandbox Code Playgroud)
我会重写这段代码以避免使用goto:
string message;
if (myArrayTable.Cast<string>().Contains(cautat)) {
message = "Found";
} else {
message = "Not found!";
}
Console.WriteLine(message);
Run Code Online (Sandbox Code Playgroud)