高分排行榜

时间:2015-02-10 18:35:11

标签: c# winforms leaderboard

我正试图将我的排行榜的得分按从高到低的顺序排列。 我不知道我会怎么做,我知道这不是一个要求编写代码的地方,但我到处寻找,我需要创建一个数组或列表,还是一个类?

namespace Coursework___Quiz
{
    public partial class frmFinal : Form
    {
        public frmFinal()
        {
            InitializeComponent();
        }

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

        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);
            //reads the first line of the file InputInfo and sets a string equal to it
            string nameScore = File.ReadAllLines(".\\InputInfo.bin").Skip(0).Take(1).First();
            //sets a textbox equal to the string nameScore
            rchBxNameScore.Text = nameScore;
            //reads the second line of the file InputInfo and sets a string equal to it
            string quizScore = File.ReadAllLines(".\\InputInfo.bin").Skip(1).Take(1).First();
            //sets a textbox equal to the string quizScore
            rchBxQuizNameScore.Text = quizScore;
        }

        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");
        }

        //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))
                {
                    //adds the information from the textboxes to the file
                    scoreInfo.WriteLine(rchBxNameScore.Text + "\t" + rchBxQuizNameScore.Text + "\t" + rchBxScore.Text);
                }
                //clears all the players score details
                rchBxNameScore.Clear();
                rchBxQuizNameScore.Clear();
                rchBxScore.Clear();
                rchBxScores.Text = File.ReadAllText(".\\scoreInfo.bin");
                //sets saved to true
                saved = true;
            }

        }
    }
}

目前,分数会在输入时保存。

1 个答案:

答案 0 :(得分:1)

您需要将分数存储在List中。可能是List<int>。然后你可以使用OrderBy方法:

IEnumerable<int> leaderboard = scores.OrderBy(x => x);

或者让他们下降:

IEnumerable<int> leaderboard = scores.OrderByDescending(x => x);

它会为您提供已排序的分数列表。然后使用集合视图(如ListBox),您可以在表单上显示列表。