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

C# 如何遍历打印List<Coordinate>集合并按指定格式输出宝可梦属性

实现方案

你直接在menu == '6'的分支中写入如下代码即可满足需求:

else if (menu == '6')
{
    // 遍历集合内所有宝可梦数据
    foreach (var pokemon in myList)
    {
        Console.WriteLine($"Pokemon's name = {pokemon.Name}");
        Console.WriteLine($"Pokemon's HP = {pokemon.HP}");
        Console.WriteLine($"Pokemon's EXP = {pokemon.EXP}");
        // 可选:不同条目之间加空行分隔,可读性更好
        Console.WriteLine();
    }
}

如果需要处理集合为空的场景,避免无输出的情况,可以加上判断逻辑:

else if (menu == '6')
{
    if (myList.Count == 0)
    {
        Console.WriteLine("暂未添加任何宝可梦数据");
        return;
    }
    foreach (var pokemon in myList)
    {
        Console.WriteLine($"Pokemon's name = {pokemon.Name}");
        Console.WriteLine($"Pokemon's HP = {pokemon.HP}");
        Console.WriteLine($"Pokemon's EXP = {pokemon.EXP}");
        Console.WriteLine();
    }
}

上述代码用到了C#的字符串插值语法,相比传统字符串拼接写法更简洁,也不容易出现格式错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 06:06:04