小编pat*_*ick的帖子

在switch和cases中分配变量

我一直在尝试为变量赋值,然后在这个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语句,但我想知道交换机和案例是如何工作的!)

c# scope

0
推荐指数
2
解决办法
386
查看次数

标签 统计

c# ×1

scope ×1