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

为何独立方法中的switch无法反馈修改后的状态信息?

问题根源与修复方案

核心问题分析

  • 值类型传递导致修改无效: C#中int是值类型,方法参数默认按值传递,因此你在response方法里修改的只是变量的副本,Main方法中的原始变量不会受到任何影响。
  • 方法内的无限循环: response方法里的while (FP > 0)循环没有更新输入的points值,一旦进入循环就会无限重复执行同一个switch分支(或卡在默认分支),无法正常退出。
  • 返回值无意义: 方法返回的是传入的points字符串,完全无法将修改后的状态反馈给Main方法。
  • 拼写错误: 原代码中AG10的case应为AGI10,否则输入AGI10时会触发默认分支。

修复方案一:使用引用参数(ref)

通过给方法参数添加ref关键字,让方法直接操作Main方法中的原始变量,同时移除方法内多余的循环(Main方法已有循环控制逻辑)。

修改后的ApplyPoints方法

static void ApplyPoints(ref int CHA, ref int STR, ref int AGI, ref int PERC, ref int END, ref int HP, ref int FP, string points)
{
    switch (points)
    {
        case "CHA1":
            CHA++;
            FP--;
            break;
        case "CHA5":
            CHA += 5;
            FP -= 5;
            break;
        case "CHA10":
            CHA += 10;
            FP -= 10;
            break;

        case "STR1":
            STR++;
            FP--;
            break;
        case "STR5":
            STR += 5;
            FP -= 5;
            break;
        case "STR10":
            STR += 10;
            FP -= 10;
            break;

        case "AGI1":
            AGI++;
            FP--;
            break;
        case "AGI5":
            AGI += 5;
            FP -= 5;
            break;
        case "AGI10": // 修正原代码中的拼写错误
            AGI += 10;
            FP -= 10;
            break;

        case "PERC1":
            PERC++;
            FP--;
            break;
        case "PERC5":
            PERC += 5;
            FP -= 5;
            break;
        case "PERC10":
            PERC += 10;
            FP -= 10;
            break;

        case "END1":
            END++;
            HP += 8;
            FP--;
            break;
        case "END5":
            END += 5;
            HP += 40;
            FP -= 5;
            break;
        case "END10":
            END += 10;
            HP += 80;
            FP -= 10;
            break;
        default:
            Console.WriteLine("That is not a stat.");
            break;
    }
}

Main方法中调用修改后的方法

将原switch代码替换为方法调用,注意每个int参数都要添加ref关键字:

static void Main(string[] args)
{
    int FP = 20;
    int CHA = 8;
    int STR = 8;
    int AGI = 8;
    int PERC = 8;
    int END = 8;
    int HP = AGI * 3;

    int thug = 10;
    int TD = 2;

    while (FP > 0)
    {
        Console.WriteLine("Your Current Stats: " + "\nCHA: " + CHA + "\nSTR: " + STR + "\nAGI: " + AGI + "\nPERC: " + PERC + "\nEND: " + END + "\nHP: " + HP);
        Console.WriteLine("You have  " + FP + " free points left.");
        Console.WriteLine("");
        Console.WriteLine("Allows for adding up to in 1, 5, and 10's. Add the number after the stat. Examples: CHA1, CHA5 CHA10");
        Console.WriteLine("");
        Console.WriteLine("Where would you like to apply your Free Points: |CHA|:Charisma, |STR| Strength, |AGI|:Aglility, |PERC|:Perception, |END|:Endurance");
        string points = Console.ReadLine();
        points = points.ToUpper();

        // 调用修改后的方法,传递引用参数
        ApplyPoints(ref CHA, ref STR, ref AGI, ref PERC, ref END, ref HP, ref FP, points);
    }
}

修复方案二:使用类封装状态(更优雅)

如果参数过多,使用引用参数会显得繁琐,更好的做法是创建一个类来封装所有角色状态:

public class CharacterStats
{
    public int FP { get; set; }
    public int CHA { get; set; }
    public int STR { get; set; }
    public int AGI { get; set; }
    public int PERC { get; set; }
    public int END { get; set; }
    public int HP { get; set; }
}

static void ApplyPoints(CharacterStats stats, string points)
{
    switch (points)
    {
        case "CHA1":
            stats.CHA++;
            stats.FP--;
            break;
        case "CHA5":
            stats.CHA += 5;
            stats.FP -= 5;
            break;
        case "CHA10":
            stats.CHA += 10;
            stats.FP -= 10;
            break;
        // 其他case逻辑与方案一一致,此处省略
        case "END10":
            stats.END += 10;
            stats.HP += 80;
            stats.FP -= 10;
            break;
        default:
            Console.WriteLine("That is not a stat.");
            break;
    }
}

// Main方法调用
static void Main(string[] args)
{
    CharacterStats stats = new CharacterStats
    {
        FP = 20,
        CHA = 8,
        STR = 8,
        AGI = 8,
        PERC = 8,
        END = 8,
        HP = 8 * 3
    };

    while (stats.FP > 0)
    {
        Console.WriteLine($"Your Current Stats: \nCHA: {stats.CHA}\nSTR: {stats.STR}\nAGI: {stats.AGI}\nPERC: {stats.PERC}\nEND: {stats.END}\nHP: {stats.HP}");
        Console.WriteLine($"You have {stats.FP} free points left.");
        Console.WriteLine("");
        Console.WriteLine("Allows for adding up to in 1, 5, and 10's. Add the number after the stat. Examples: CHA1, CHA5 CHA10");
        Console.WriteLine("");
        Console.WriteLine("Where would you like to apply your Free Points: |CHA|:Charisma, |STR| Strength, |AGI|:Aglility, |PERC|:Perception, |END|:Endurance");
        string points = Console.ReadLine().ToUpper();
        ApplyPoints(stats, points);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 21:09:51