C# 在比赛中得分很高

C# 在比赛中得分很高,c#,C#,基本上,我习惯于通过列表框以500比1的顺序显示高分。这是我的代码,请记住label1是游戏中的分数,所以如果有人可以帮助我吗 { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void Form3_Load(object sender, EventArgs e) { label1.Text

基本上,我习惯于通过列表框以500比1的顺序显示高分。这是我的代码,请记住label1是游戏中的分数,所以如果有人可以帮助我吗

{
public partial class Form3 : Form
{
    public Form3()

    {
        InitializeComponent();
    }




    private void Form3_Load(object sender, EventArgs e)
    {
        label1.Text = Form2.passingText;

        StreamWriter q = new StreamWriter("C:\\Users\\BS\\Desktop\\tex.txt", true);
        q.WriteLine(label1.Text);
        q.Close();


        StreamReader sr = new StreamReader("C:\\Users\\BS\\Desktop\\tex.txt");
        string g = sr.ReadLine();
        while (g != null)
        {
            listBox1.Items.Add(g);

            g = sr.ReadLine();
        }
        sr.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

}

您可以将文件作为行列表读取,然后使用Linq对其进行排序 因此,与其使用SteamReader,不如尝试以下方法:

using System.Linq;
//....

List<string> hiscores = File.ReadAllLines("C:\\Users\\BS\\Desktop\\tex.txt").ToList();
hiscores.Sort();
foreach (string s in hiscores)
    listBox1.Items.Add(s);
编辑: 由于您必须使用StreamReader,以下是此方法,但原理相同:

    List<string> hiscores = new List<string>();
    StreamReader sr = new StreamReader("C:\\Users\\BS\\Desktop\\tex.txt");
    string g = sr.ReadLine();
    while (g != null)
    {
        hiscores.Add(g);
        g = sr.ReadLine();
    }
    sr.Close();
    hiscores.Sort();
    hiscores.Reverse();
    //alternatively, instead of Sort and then reverse, you can do
    //hiscores.OrderByDescending(x => x);

    foreach(string s in hiscores)
    {
        listBox1.Items.Add(s);
    }

我感谢您的帮助,但我需要使用StreamReader来完成这个项目,所以如果有人知道怎么做的话?再次感谢您,但我可以通过选择sort to true来实现升序,但我需要按照降序排列,就像我的分数为420、490、120、320一样,就像490、420、320、120一样,我是乞丐,所以也许你的代码是好的,但我没有正确地使用它,或者你误解了我。。还有其他有解决方案的吗?请重新编辑我的答案。排序后,您对其核心进行反向排序。反向。现在试试。谢谢你,你是我的英雄!谢谢你的耐心。我也试着用倒转来做同样的事情,但却把它改成了高分。倒转就像你吼叫高分一样。排序我已经替换了它,再次感谢你!标记我的答案,因为解决方案是表达感谢的最佳方式:如果您向我们展示label1.Text或tex.txt包含的内容,它将非常有帮助。label1.Text包含您在游戏中获得的分数。。。tex.txt是一个带有StreamReader和Writer的文件连接器,用于保存这些分数,因为我必须在这个项目中使用StreamReader和Writer。谢谢你,再见