我的C#循环做错了什么

本文关键字:错了 什么 循环 我的 | 更新日期: 2023-09-27 18:29:15

好的,所以我有两个列表

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,否则我想用不同的颜色。。这是我到目前为止的代码

        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的项目来说是坏的……关于如何用我的代码实现这一点的任何想法

我的C#循环做错了什么

实现这一点非常容易——只需在检查过程中构建另一个列表而不是计数器,然后检查"innerlist"的大小。我称之为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。