C# 如果球员从评分中“脱颖而出”,如何删除他的成绩?

C# 如果球员从评分中“脱颖而出”,如何删除他的成绩?,c#,winforms,C#,Winforms,我需要使玩家从字典和文件中删除,如果他飞出评级,即,第六名 我有一个想法通过字典来完成。删除它,然后在文件中覆盖它 我做了一个动作算法: 读取文件,删除内存中的行,然后将内容返回到文件。如果文件较大,可以读取该行的行并创建临时文件,稍后替换原始文件 下面是将它们分类到不同位置的代码,也许有些东西会有所帮助: private static Dictionary<string, int> AllNames() { return File .Re

我需要使玩家从字典和文件中删除,如果他飞出评级,即,第六名 我有一个想法通过字典来完成。删除它,然后在文件中覆盖它

我做了一个动作算法: 读取文件,删除内存中的行,然后将内容返回到文件。如果文件较大,可以读取该行的行并创建临时文件,稍后替换原始文件

下面是将它们分类到不同位置的代码,也许有些东西会有所帮助:

private static Dictionary<string, int> AllNames()
    {
        return File
          .ReadLines(@"C:\Users\HP\Desktop\картинки\results.txt")
          .Where(line => !string.IsNullOrWhiteSpace(line))
          .Select(item => item.Split(' '))
          .ToDictionary(items => items[0],
                        items => int.Parse(items[1]));
    }


    private void updateRatingLabels()
    {
        var tops = AllNames()
          .OrderBy(pair => pair.Value)                      
          .ThenBy(pair => pair.Key, StringComparer.Ordinal) 
          .Take(5)                                           
          .ToArray();


        for (int i = 18; i <= 22; ++i)
            Controls.Find($"label{i}", true).First().Text = "";

        for (int i = 28; i <= 32; ++i)
            Controls.Find($"label{i}", true).First().Text = "";


        for (int i = 0; i < tops.Length; ++i)
        {
            Controls.Find($"label{i + 18}", true).First().Text = tops[i].Key;
            Controls.Find($"label{i + 28}", true).First().Text = $"{tops[i].Value / 60}:{tops[i].Value % 60:00}";
        }
    }

让我们用另一种方式来回答这个问题:我们最多不会删除5名顶级球员。我们必须考虑在领结的情况下我们应该做什么,即2个或更多的球员有相同的分数。我认为诚实的做法是保留所有这样的播放器,以便实际字典可以超过5条记录:

要将数据保存到文件中,请尝试file.writeAllines;让我们使用名称-值格式

编辑:如果我们必须添加一个用户名和分数,我们可以通过两种方式进行:

无论分数是多少,删除第5名并添加新用户:

 var newDictionary = AllNames()
  .GroupBy(pair => pair.Value)  // groups by scores                    
  .OrderBy(chunk => chunk.Key)  // less seconds the better
  .Take(4)                      // at most 4 groups (ties preserved)                     
  .SelectMany(chunk => chunk)   // flatten back ("ungroup")
  .ToDictionary(pair => pair.Key, 
                pair => pair.Value);

 newDictionary.Add(name, score);
添加新用户,然后记下前5名,新用户可以作为较低的执行者被排除在外

 var newDictionary = AllNames()
  .Concat(new KeyValuePair<string, int>[] { 
     new KeyValuePair<string, int>(name, score)} // All new users here
   )
  .GroupBy(pair => pair.Value)  // groups by scores                    
  .OrderBy(chunk => chunk.Key)  // less seconds the better
  .Take(5)                      // at most 5 groups (ties preserved)                     
  .SelectMany(chunk => chunk)   // flatten back ("ungroup")
  .ToDictionary(pair => pair.Key, 
                pair => pair.Value);
您可以在某处调用,例如,单击按钮:

 private void saveButton_Click(object sender, EventArgs e) {
   SaveNewUser(textBoxWithName.Text, int.Parse(textBoxWithScore.Text));
 }

您面临的问题是什么?例如,如果第五名因第六名而降低,则应将其从文件和字典中删除,但如何删除我不知道AllName中存在哪种数据?下面的代码是多余的,因为您正在覆盖for循环中的值。对于int i=18;i@Lines:假设你有palyers a1,b1,c1,d2,e2,f2,g3。我建议诚实地保留它们中的A1、B1、C1、D2、E2、F2-6而不排除F2。很可能你不理解我需要的任务的本质:1。这样一来,他就有了一个新的录音机,他被加入了评级,而最后一个排名第五的球员,也就是说,他离开了评级。他的记录不再显示。您需要删除这个播放器。@Lines:您应该将代码放入负责添加新用户的例程。@Lines:private void updateRatingLabels2{var newDictionary=…;File.writeAllines…;}对不起,我显然没有制定好目标。有一种字典,它的元素取自一个文件,并根据从另一个表单输入的数据写入一个文件。目标是:这里有全部5个位置,也就是说,所有5个位置都分布在玩家之间,我玩6个游戏并击败某人
 var newDictionary = AllNames()
  .Concat(new KeyValuePair<string, int>[] { 
     new KeyValuePair<string, int>(name, score)} // All new users here
   )
  .GroupBy(pair => pair.Value)  // groups by scores                    
  .OrderBy(chunk => chunk.Key)  // less seconds the better
  .Take(5)                      // at most 5 groups (ties preserved)                     
  .SelectMany(chunk => chunk)   // flatten back ("ungroup")
  .ToDictionary(pair => pair.Key, 
                pair => pair.Value);
 private void SaveNewUser(string name, int score) {
   var newDictionary = AllNames()
     .GroupBy(pair => pair.Value)     // groups by scores                    
     .OrderBy(chunk => chunk.Key)     // less seconds the better
     .Take(4)                         // at most 4 groups (ties preserved)         
     .SelectMany(chunk => chunk)      // flatten back ("ungroup")
     .ToDictionary(pair => pair.Key, 
                  pair => pair.Value);

   newDictionary.Add(name, score);

   File.WriteAllLines(@"c:\TopPlayers.txt", newDictionary
     .Select(pair => $"{pair.Key} {pair.Value}"));
 }
 private void saveButton_Click(object sender, EventArgs e) {
   SaveNewUser(textBoxWithName.Text, int.Parse(textBoxWithScore.Text));
 }