【问题标题】:Sorting a leaderboard from highest to lowest将排行榜从高到低排序
【发布时间】:2015-04-13 19:26:22
【问题描述】:

我正在尝试在richTextBox 中按从高到低的顺序排列测验的分数。我将球员姓名、测验和分数保存在一个名为scoresClass 的类中。在测验结束时,我给全班三个richTextBoxs 打电话,显示他们的名字、测验和分数。然后我将它们添加到列表中并将列表写入文件。排行榜为richTextBox,设置为等于文件中的数据。

这是我的代码:

public frmFinal()
{
    InitializeComponent();            
}

private void frmFinal_FormClosed(object sender, FormClosedEventArgs e)
{
    Application.Exit();
}

List<string> scores = new List<string>();

private void frmFinal_Load(object sender, EventArgs e)
{
    //sets the leaderboard equal to the file scoreInfo
    rchBxScores.Text = File.ReadAllText(".\\scoreInfo.bin");
    //sets a textbox equal to the players score
    rchBxScore.Text = Convert.ToString(scoresClass.score);
    rchBxNameScore.Text = scoresClass.name;
    rchBxQuizNameScore.Text = scoresClass.quiz;
}

private void btnClearScores_Click(object sender, EventArgs e)
{
    //opens the file scoreInfo
    FileStream fileStream = File.Open(".\\scoreInfo.bin", FileMode.Open);
    //empties the file
    fileStream.SetLength(0);
    //closes the file
    fileStream.Close();
    //sets the leaderbaord equal to the file
    rchBxScores.Text = File.ReadAllText(".\\scoreInfo.bin");
    scores.Clear();
}

//creates a bool variable and sets it equal to false
bool saved = false;

private void btnSaveScore_Click(object sender, EventArgs e)
{
    //checks if saved equals false
    if (saved == false)
    {
        //if saved equals false, it opens the file scoreInfo
        using (StreamWriter scoreInfo = new StreamWriter(".\\scoreInfo.bin", true))
        {
            scores.Add(scoresClass.name + "\t" + scoresClass.quiz + "\t" + scoresClass.score);

            foreach(string score in scores)
            {                        
                scoreInfo.WriteLine(score);                        
            }                    
        }

        //clears all the players score details
        rchBxNameScore.Clear();
        rchBxQuizNameScore.Clear();
        rchBxScore.Clear();
        rchBxScores.Text = File.ReadAllText(".\\scoreInfo.bin");
        //sets saved to true
        saved = true;
    }            
}

目前分数是按输入时间计算的,而不是按分数计算的。我不确定我会如何实际订购它们。

这是课程:

public class scoresClass
{
    public static int score = 0;
    public static string name = "";
    public static string quiz = "";

    public scoresClass(string userName, int userScore, string userQuiz)
    {
        name = userName;
        score = userScore;
        quiz = userQuiz;
    }
}

【问题讨论】:

  • 我喜欢这个评论://creates a bool variable and sets it equal to false 严肃地说,scoresClass 是什么,它在哪里定义/填充?
  • @MichaelMcGriff 它是一个类,它包含玩家的姓名、测验名称和分数。
  • 你能贴出显示scoresClass的定义和人口的代码吗?
  • @MichaelMcGriff 刚刚添加
  • 您使用的是List&lt;scoresClass&gt;() 吗?我找不到您在遍历分数集合的任何地方。我看到你使用 scoresClass 的唯一地方你把它当作单身人士对待。

标签: c# string list class leaderboard


【解决方案1】:

由于您使用StreamWriter 附加到文件,我会将文件作为scoreClass 的集合读回,而不是盲目地读取文件并将其转储到richTextBox 中。类似的东西。

我也必须更改你的类定义。

public class scoresClass
{
    public int score = 0;
    public string name = "";
    public string quiz = "";

    public scoresClass(string userName, string userQuiz, int userScore)
    {
        name = userName;
        score = userScore;
        quiz = userQuiz;
    }
}

private List<scoresClass> importScoresFromFile(string path)
{
    var listOfScores = new List<scoresClass>();

    var rawScores = File.ReadAllLines(path);

    foreach (var score in rawScores)
    {
        string[] info = score.Split('\t');

        listOfScores.Add(new scoresClass(info[0], info[1], Convert.ToInt32(info[2])));
    }

    return listOfScores.OrderByDescending(r => r.score).ToList();
}

一旦您将所有分数都记入内存中,您就可以做一些LINQ 的工作来对它们进行排序。然后,您可以遍历List&lt;scoresClass&gt; 中的每个值并根据需要导出到richTextBox

【讨论】:

  • 谢谢!这很有帮助。
最近更新 更多