我在C#循环中做错了什么

时间:2012-03-27 20:05:53

标签: c#

好的,我有2个清单

List<string> playlists 
List<string> sync

并且让我们说播放列表的内容是三个字符串

{"more and you", "and us", "more"}

并且同步的内容是这个

{"more and you-20120312", "more and you-20120314", "more and you-20120313",  "and us-20120313",  "and us-20120314",  "more-20120314",  "more-20120313", "more-20120312"}

基本上我想做的是遍历播放列表中的所有循环并找到相关的同步并将它们打印出来然后它们需要为3,否则我想以不同的颜色对它们进行着色。所以这是我的代码到目前为止< / p>

        StringBuilder sb = new StringBuilder();
        sb.Append("<h2>Playlist Information</h2>");
        foreach (string play in playlists)
        {
            int counter = 0;
            foreach (string s in sync)
            {
                if (s.StartsWith(play))
                {
                    sb.Append("<p class=\"good\">" + s + "</p>");
                    counter++;
                } 
            }
        }

所以我希望最终的html看起来像这样

<h2>Playlist Information</h2>
<p class=\"good\">more and you-20120312</p>
<p class=\"good\">more and you-20120313</p>
<p class=\"good\">more and you-20120314</p>
<p class=\"bad\">and us-20120313</p>
<p class=\"bad\">and us-20120314</p>
<p class=\"good\">more-20120312</p>
<p class=\"good\">more-20120313</p>
<p class=\"good\">more-20120314</p>

对于不符合至少3项的项目的不利...有关如何使用我的代码实现此目的的任何想法

1 个答案:

答案 0 :(得分:1)

实现这一点非常容易 - 只需在检查期间建立另一个列表而不是计数器,然后检查“内部列表”的大小。我称之为currentSyncSet:

    static void Main(string[] args)
    {
        List<string> playlists = new List<string>(){"more and you", "and us", "more"};
        List<string> sync = new List<string>() { "more and you-20120312", "more and you-20120314", "more and you-20120313", "and us-20120313", "and us-20120314", "more-20120314", "more-20120313", "more-20120312" };

        StringBuilder sb = new StringBuilder();
        sb.Append("<h2>Playlist Information</h2>\r\n");

        HashSet<string> finalSyncResult = new HashSet<string>();

        foreach (string play in playlists)
        {
            List<string> currentSyncSet = new List<string>();

            foreach (string s in sync)
            {
                if (s.StartsWith(play))
                {
                    currentSyncSet.Add(s);
                }
            }

            foreach (var syncset in currentSyncSet)
            {
                if (currentSyncSet.Count < 3)
                {
                    finalSyncResult.Add("<p class=\"bad\">" + syncset + "</p>");
                }
                else
                {
                    finalSyncResult.Add("<p class=\"good\">" + syncset + "</p>");
                }
            }
        }

        foreach (var result in finalSyncResult)
        {
            sb.Append(result + "\r\n");
        }

        Console.WriteLine(sb.ToString());

        Console.ReadKey();
    }

输出是:

<h2>Playlist Information</h2>
<p class="good">more and you-20120312</p>
<p class="good">more and you-20120314</p>
<p class="good">more and you-20120313</p>
<p class="bad">and us-20120313</p>
<p class="bad">and us-20120314</p>
<p class="good">more-20120314</p>
<p class="good">more-20120313</p>
<p class="good">more-20120312</p>

问候

更新1: Sry,上次,我忘记了,你不想有重复的条目 - 因此我在这个解决方案中添加了一个HashSet。