You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中如何在switch分支赋值变量并在while循环外部访问?

问题:如何在while循环外部访问switch分支内定义的Pokemon变量?

我是编程新手,正在复刻宝可梦游戏的开头部分练习编程。我用while循环结合switch分支实现宝可梦选择功能(注:已编写TypeLine方法实现逐字符打印文本,可视为Console.WriteLine)。现有代码如下:

//Player makes their Pokemon choice.
bool pokemonChoice = true;

while (pokemonChoice)
{
    string input = Console.ReadLine();
    int key = Convert.ToInt32(input);

    switch (key)
    {
        case 1:
            TypeLine("\nCharmander! What an excellent decision! \n");
            TypeLine("CONGRATULATIONS! YOU JUST OBTAINED CHARMANDER! \n");
            pokemonChoice = false;
            break;
        case 2:
            TypeLine("\nSquirtle! What an excellent decision! \n");
            TypeLine("CONGRATULATIONS! YOU JUST OBTAINED SQUIRTLE! \n");
            pokemonChoice = false;
            break;
        case 3:
            TypeLine("\nBulbasaur! What an excellent decision! \n");
            TypeLine("CONGRATULATIONS! YOU JUST OBTAINED BULBASAUR! \n");
            pokemonChoice = false;
            break;
    }
}

我希望实现:用户输入1选择case 1时,创建名为pokemon的字符串变量并赋值为"Charmander",后续能在代码中使用该变量,比如:

Console.WriteLine(pokemon + " I choose you!");

我尝试在case分支内定义该变量,代码如下:

switch (key)
{
    case 1:
        string pokemon = ("Charmander");
        TypeLine("\nCharmander! What an excellent decision! \n");
        TypeLine("CONGRATULATIONS! YOU JUST OBTAINED CHARMANDER! \n");
        pokemonChoice = false;
        break;
}

但在while循环外部访问该变量时,出现错误提示:"The name 'pokemon' does not exist in the current context"。请问如何实现预期效果?

解决方案:调整变量作用域
  • 问题根源:你在case分支里定义的pokemon变量,作用域只限于当前case代码块内部,循环外部无法识别这个变量。要让外部能访问,必须把变量定义在while循环之外、switch之前的位置,扩大它的作用域范围。

  • 修改步骤:

    1. 在bool pokemonChoice = true;之后,提前定义string pokemon = "";(也可以用null初始化,字符串类型推荐给默认空值)。
    2. 在每个case分支里,直接给这个已经定义好的pokemon变量赋值,不要重新定义变量。

修改后的完整代码示例:

//Player makes their Pokemon choice.
bool pokemonChoice = true;
string pokemon = ""; // 将变量定义在循环外部,确保作用域覆盖后续代码

while (pokemonChoice)
{
    string input = Console.ReadLine();
    int key = Convert.ToInt32(input);

    switch (key)
    {
        case 1:
            pokemon = "Charmander"; // 直接赋值,不再重新定义变量
            TypeLine("\nCharmander! What an excellent decision! \n");
            TypeLine("CONGRATULATIONS! YOU JUST OBTAINED CHARMANDER! \n");
            pokemonChoice = false;
            break;
        case 2:
            pokemon = "Squirtle";
            TypeLine("\nSquirtle! What an excellent decision! \n");
            TypeLine("CONGRATULATIONS! YOU JUST OBTAINED SQUIRTLE! \n");
            pokemonChoice = false;
            break;
        case 3:
            pokemon = "Bulbasaur";
            TypeLine("\nBulbasaur! What an excellent decision! \n");
            TypeLine("CONGRATULATIONS! YOU JUST OBTAINED BULBASAUR! \n");
            pokemonChoice = false;
            break;
        default:
            // 添加默认分支处理无效输入,避免程序卡循环
            TypeLine("请输入1、2或3来选择你的宝可梦!\n");
            break;
    }
}

// 现在循环外部可以正常访问pokemon变量了
Console.WriteLine(pokemon + " I choose you!");
  • 额外优化:建议加上default分支处理用户输入错误的情况,提升程序的健壮性。

内容的提问来源于stack exchange,提问作者WolfPG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 04:54:16