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

如何打印包含列表的Dictionary?C#开发求助

宾客喜好列表格式化输出解决方案

输入示例

Like Krisi shrimps
Like Krisi soup
Like Penelope dessert
Like Misho salad
stop

原代码

using System;
using System.Collections.Generic;
using System.Linq;

namespace GuestsFoods._1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            int unliked = 0;
            Dictionary<string, List<string>> guestsFoods = new Dictionary<string, List<string>>(); 
            
            while(input != "stop")
            {
                List<string> inputs = input.Split(" ").ToList();
                if (inputs[0] == "Like")
                {
                    if (guestsFoods.ContainsKey(inputs[1]))
                    {
                        guestsFoods[inputs[1]].Add(inputs[2]);
                    }
                    else
                    {
                        guestsFoods.Add(inputs[1], new List<string> {inputs[2]});
                    }

                }
                else if (inputs[0] == "Dislike")
                {
                    // 原逻辑错误:判断条件写反
                    if (guestsFoods!.ContainsKey(inputs[1]))
                    {
                        Console.WriteLine($"{inputs[1]} is not at the party");
                    }
                    else
                    {
                        guestsFoods[inputs[1]].Remove(inputs[2]);
                        unliked++;
                    }
                }
                input = Console.ReadLine();
            }
            // 未完成的输出逻辑
            //foreach (KeyValuePair<string, List<string>> data in guestsFoods)
            //{
                //Console.WriteLine(data.Key + " -> ");
                //foreach (string food in guestsFoods[new List<string>[]])
                //{
                    //Console.Write("");
                //}
            //}
        }
    }
}

需求说明

用Dictionary的Key存储人名,Value(List<string>类型)存储对应宾客喜欢的食物,最终输出格式要求:

Key - Value1, Value2, Value3 ... ValueN

修改后的完整代码

using System;
using System.Collections.Generic;
using System.Linq;

namespace GuestsFoods._1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            int unliked = 0;
            Dictionary<string, List<string>> guestsFoods = new Dictionary<string, List<string>>(); 
            
            while(input != "stop")
            {
                List<string> inputs = input.Split(" ").ToList();
                if (inputs[0] == "Like")
                {
                    if (guestsFoods.ContainsKey(inputs[1]))
                    {
                        guestsFoods[inputs[1]].Add(inputs[2]);
                    }
                    else
                    {
                        guestsFoods.Add(inputs[1], new List<string> {inputs[2]});
                    }

                }
                else if (inputs[0] == "Dislike")
                {
                    // 修正宾客存在性判断逻辑
                    if (!guestsFoods.ContainsKey(inputs[1]))
                    {
                        Console.WriteLine($"{inputs[1]} is not at the party");
                    }
                    else
                    {
                        // 新增食物存在性检查,避免无效移除
                        if (guestsFoods[inputs[1]].Contains(inputs[2]))
                        {
                            guestsFoods[inputs[1]].Remove(inputs[2]);
                            unliked++;
                        }
                        else
                        {
                            Console.WriteLine($"{inputs[1]} doesn't like {inputs[2]}");
                        }
                    }
                }
                input = Console.ReadLine();
            }

            // 实现格式化输出逻辑
            foreach (var guestEntry in guestsFoods)
            {
                // 将食物列表拼接为逗号分隔的字符串
                string foodsStr = string.Join(", ", guestEntry.Value);
                Console.WriteLine($"{guestEntry.Key} - {foodsStr}");
            }
        }
    }
}

关键修改说明

  1. 修正Dislike分支逻辑:原代码中宾客存在性判断条件写反,改为!guestsFoods.ContainsKey(inputs[1]);同时新增食物存在性检查,避免对不存在的食物执行移除操作。
  2. 实现格式化输出:
    • 遍历Dictionary的每个键值对
    • 使用string.Join(", ", guestEntry.Value)将食物列表转为逗号分隔的字符串
    • 按照要求的格式拼接并输出每一行内容

测试输出

针对给定的输入示例,运行后输出:

Krisi - shrimps, soup
Penelope - dessert
Misho - salad

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 18:05:34