我正在制作假的shell程序,我希望能够通过输入一行代码来触发错误屏幕。
这是我的代码的受影响的一部分:
Console.Title = "Command Prompt";
OperatingSystem os = Environment.OSVersion;
Version ver = os.Version;
ver.ToString();
if (os.VersionString.Equals("Microsoft Windows NT 6.2.9200.0"))
{
Console.WriteLine("Microsoft Windows [Version 6.3.9600]");
Console.WriteLine("(c) 2013 Microsoft Corporation. All rights reserved. \n");
goto a;
}
else
ver_error:
{
Console.WriteLine("Error: OS '" + os.VersionString + "' Missing from libraries, \nnot yet implemented?\n");
Console.WriteLine("Available Operating Systems:\nWndows 8.1, Microsoft Windows NT 6.2.9200.0 ");
Thread.Sleep(3000);
Console.WriteLine("\n\nPress enter to boot Error Protocol, type exit to exit.\n");
error_test:
string errorProtocol;
errorProtocol = Console.ReadLine();
if (errorProtocol.Equals(""))
{
Console.WriteLine("Booting Error Protocol...");
Thread.Sleep(2000);
goto error_report;
}
else if (errorProtocol.Equals("exit"))
{
goto end;
}
else
{
Console.WriteLine("Not a valid response.");
goto error_test;
}
}
/* C:\Users\18LeunJA dir */
a:
Console.Write("C:\\Users\\18LeunJA>");
string userValueFromStart;
userValueFromStart = Console.ReadLine();
if (userValueFromStart.Equals("exit"))
{
goto end;
}
else if (userValueFromStart.Equals("triggerOSError"))
{
goto ver_error;
}
受影响的代码是“ver_error:”行。谁能告诉我为什么这不起作用?
编辑:我试图在else体内移动ver_error,但是它说没有引用标签,并且在goto语句的范围内有'没有这样的标签'ver_error'。
在外面时,会出现错误'嵌入式声明不能是声明或带标签的声明'。
答案 0 :(得分:0)
我认为你的代码应该是代码:
尝试在else
else
的括号
Console.Title = "Command Prompt";
OperatingSystem os = Environment.OSVersion;
Version ver = os.Version;
ver.ToString();
if (os.VersionString.Equals("Microsoft Windows NT 6.2.9200.0"))
{
Console.WriteLine("Microsoft Windows [Version 6.3.9600]");
Console.WriteLine("(c) 2013 Microsoft Corporation. All rights reserved. \n");
goto a;
}
else
{
ver_error:
Console.WriteLine("Error: OS '" + os.VersionString + "' Missing from libraries, \nnot yet implemented?\n");
Console.WriteLine("Available Operating Systems:\nWndows 8.1, Microsoft Windows NT 6.2.9200.0 ");
Thread.Sleep(3000);
Console.WriteLine("\n\nPress enter to boot Error Protocol, type exit to exit.\n");
error_test:
string errorProtocol;
errorProtocol = Console.ReadLine();
if (errorProtocol.Equals(""))
{
Console.WriteLine("Booting Error Protocol...");
Thread.Sleep(2000);
goto error_report;
}
else if (errorProtocol.Equals("exit"))
{
goto end;
}
else
{
Console.WriteLine("Not a valid response.");
goto error_test;
}
}
/* C:\Users\18LeunJA dir */
a:
Console.Write("C:\\Users\\18LeunJA>");
string userValueFromStart;
userValueFromStart = Console.ReadLine();
if (userValueFromStart.Equals("exit"))
{
goto end;
}
else if (userValueFromStart.Equals("triggerOSError"))
{
goto ver_error;
}
顺便说一句goto
只能在同一范围内工作。您从goto
部分到if
部分致电else
- 这是错误的。