Професионална програма
Loading...
VesselinTonchev avatar VesselinTonchev 5 Точки

string.Join() не работи

Здравейте, колеги,

Може ли някой да погледне кода и да ми каже как да подкарам string.join(), заощто, каквото и да опитам, както и да го завъртя не иска и не иска.

Задачата е Nikulden's meals от exam preparation - a:
 

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

namespace Nikulden_s_meals
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> input = Console.ReadLine().Split("-").ToList();

            Dictionary<string, List<string>> guestList
                = new Dictionary<string, List<string>>();
            int counter = 0;
            int countUnlike = 0;
            string command = "";
            string guest = "";
            string meal = "";

            while (input[0] != "Stop")
            {
                command = input[0];
                guest = input[1];
                meal = input[2];

                switch (input[0])
                {
                    case "Like":
                        LikeMeal(guestList, guest, meal);
                        break;
                    case "Unlike":
                        if (guestList.ContainsKey(guest))
                        {
                            if (guestList[guest].Contains(meal))
                            {
                                guestList[guest].Remove(meal);
                                Console.WriteLine($"{guest} doesn't like the {meal}.");
                                countUnlike++;
                            }
                            else
                            {
                                Console.WriteLine($"{guest} doesn't have the " +
                                    $"{meal} in his/her collection.");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"{guest} is not at the party.");                            
                        }
                        break;
                }

                input = Console.ReadLine().Split("-").ToList();
            }
            Output(guestList, counter, ref countUnlike);
        }

        private static void LikeMeal(Dictionary<string, List<string>> guestList, string guest, string meal)
        {
            if (!guestList.ContainsKey(guest))
            {
                guestList.Add(guest, new List<string>());

                if (!guestList[guest].Contains(meal))
                {
                    guestList[guest].Add(meal);
                }
            }
            else
            {
                guestList[guest].Add(meal);
            }
        }

        private static int Output(Dictionary<string, List<string>> guestList, int counter,ref int countUnlike)
        {
            int guestCount = 0;

            foreach (var name in guestList.OrderByDescending(x => x.Value.Count))
            {
                Console.Write($"{name.Key}:");
                guestCount++;

                foreach (var item in name.Value)
                {
                    var result = string.Join(", ", item);
                    Console.Write(result);
                    counter++;

                    //if (counter < name.Value.Count)
                    //{
                    //    Console.Write(", ");
                    //}
                }

                if (guestCount < guestList.Count || name.Value.Count >= 0)
                {
                    Console.WriteLine();
                }
            }
            Console.WriteLine($"Unliked meals: {countUnlike}");

            return counter;
        }
    }
}

 

Тагове:
0
Fundamentals Module
VesselinTonchev avatar VesselinTonchev 5 Точки

Разбрах ви. Махнах втория foreach и се получи. Изрежда колекция, а не единичен item:D 

 

Благодаря ви! 

0