我正在添加一个功能来跟踪我制作的小游戏的分数.我想从包含分数的文件中获得前5个分数(包括该分数的名称).
保存分数的格式为:
[名] - [评分]
分数和名称存储在2个列表中,我用这种方式解析:
string scores = File.ReadAllText(Environment.GetEnvironmentVariable("TEMP") + "/scores");
string[] splitscores = scores.Split('\n');
foreach (string entry in splitscores)
{
string replace = entry.Replace("[", "").Replace("]", "");
string[] splitentry = replace.Split('-');
if (splitentry.Count() > 1)
{
scorenames.Add(splitentry[0]);
scorelist.Add(Int32.Parse(splitentry[1]));
}
}
Run Code Online (Sandbox Code Playgroud)
然后我使用以下方法检索#1玩家:
int indexMax
= !scorelist.Any() ? -1 :
scorelist
.Select((value, index) => new { Value = value, Index = index })
.Aggregate((a, b) => (a.Value > b.Value) ? a : b)
.Index;
lblhighscore1.Text = "#1: " + scorelist[indexMax] + …Run Code Online (Sandbox Code Playgroud)