这段 C# 代码很简单:
这段代码的场景:
当单词匹配时,会导致意外输出:
找到单词 未找到单词 found 的值为:True
当单词不匹配时,它会产生预期的输出:
单词未找到 找到的值为:False
static void Main()
{
string[] words = { "casino", "other word" };
Boolean found = false;
foreach (string word in words)
{
if (word == "casino")
{
found = true;
goto Found;
}
}
if(!found)
{
goto NotFound;
}
Found: { Console.WriteLine("The word is found"); }
NotFound: { Console.WriteLine("The word is not found"); }
Console.WriteLine("The value of found is: {0}", found);
Console.Read();
}