我一直在尝试为变量赋值,然后在这个switch/case语句之外使用相同的变量和值.
它表示switch语句之外的变量没有在本地定义,并且想知道是否有可能使这个变量成为全局变量.
到目前为止,这是我的代码:
bool play;
string choice;
string guess;
int intChoice;
int intguess;
do
{
Console.WriteLine("1: New game\n2:quit");
choice = Console.ReadLine();
intChoice = Convert.ToInt32(choice);
switch (intChoice)
{
case 1:
play = true;
break;
case 2:
play = false;
break;
}
Console.WriteLine(play);
Run Code Online (Sandbox Code Playgroud)
(请注意,我知道我可以使用if语句,但我想知道交换机和案例是如何工作的!)