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

如何从文本文件创建并填充Monkey类对象列表(C#)

解决方案

1. 确认Monkey类定义(参考示例)

先确保你的Monkey类包含所需属性和方法,适配猴子数据的结构:

public class Monkey
{
    public int Id { get; set; }
    public List<long> Items { get; set; } = new List<long>();
    // 用Func存储运算逻辑,接收当前物品值返回新值
    public Func<long, long> Operation { get; set; }
    public int TestDivisor { get; set; }
    public int TrueTargetMonkeyId { get; set; }
    public int FalseTargetMonkeyId { get; set; }
    public int InspectCount { get; set; } = 0;

    // 示例方法:检查物品并返回目标猴子ID
    public int InspectItem(ref long itemValue)
    {
        InspectCount++;
        // 执行运算规则
        itemValue = Operation(itemValue);
        // 按题目需求调整(比如除以3)
        itemValue /= 3;
        // 判断投掷目标
        return itemValue % TestDivisor == 0 ? TrueTargetMonkeyId : FalseTargetMonkeyId;
    }
}

2. 解析Data.txt创建Monkey对象

核心是按猴子的信息块逐段解析文本,代码示例:

var monkeys = new List<Monkey>();
var allLines = File.ReadAllLines("Data.txt");

// 过滤空行,避免格式干扰
var nonEmptyLines = allLines.Where(line => !string.IsNullOrWhiteSpace(line)).ToList();

// 每5行对应一个猴子的完整数据
for (int i = 0; i < nonEmptyLines.Length; i += 5)
{
    var monkey = new Monkey();
    
    // 解析ID:格式 "Monkey X:"
    var idParts = nonEmptyLines[i].Trim().Split(' ');
    monkey.Id = int.Parse(idParts[1].TrimEnd(':'));
    
    // 解析初始物品:格式 "Starting items: 79, 98"
    var itemsStr = nonEmptyLines[i+1].Trim().Replace("Starting items: ", "");
    monkey.Items = itemsStr.Split(',')
                            .Select(s => long.Parse(s.Trim()))
                            .ToList();
    
    // 解析运算规则:格式 "Operation: new = old * 19" 或 "old + 5"
    var opStr = nonEmptyLines[i+2].Trim().Replace("Operation: new = ", "");
    if (opStr == "old * old")
    {
        monkey.Operation = old => old * old;
    }
    else if (opStr.Contains("*"))
    {
        var value = long.Parse(opStr.Split('*')[1].Trim());
        monkey.Operation = old => old * value;
    }
    else if (opStr.Contains("+"))
    {
        var value = long.Parse(opStr.Split('+')[1].Trim());
        monkey.Operation = old => old + value;
    }
    
    // 解析测试除数:格式 "Test: divisible by 23"
    monkey.TestDivisor = int.Parse(nonEmptyLines[i+3].Trim().Replace("Test: divisible by ", ""));
    
    // 解析True目标猴子:格式 "If true: throw to monkey 2"
    monkey.TrueTargetMonkeyId = int.Parse(nonEmptyLines[i+4].Trim().Replace("If true: throw to monkey ", ""));
    
    // 解析False目标猴子:格式 "If false: throw to monkey 3"
    monkey.FalseTargetMonkeyId = int.Parse(nonEmptyLines[i+5].Trim().Replace("If false: throw to monkey ", ""));
    
    monkeys.Add(monkey);
}

3. 关键注意点

  • 运算逻辑存储:用Func<long, long>替代字符串拼接执行运算,既安全又便于后续直接调用类方法。
  • 数据类型选择:物品值后期会快速增大,用long替代int防止数值溢出。
  • 格式容错:如果你的Data.txt用空行分隔猴子,先过滤空行再分组解析,避免索引越界。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 02:46:03