2017-02-25 65 views
-3

Bassicaly我被显示高分通过列表框降序排列(如500到1)。这里是我的代码,请记住label1是游戏中的得分,所以如果有人可以帮助我?游戏中的C#高分

{ 
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(); 
    } 
} 

}

+0

如果您向我们展示了包含“label1.Text”或“tex.txt”的内容,它确实会有所帮助。 –

+0

label1.Text包含您在游戏中获得的分数......并且tex.txt是一个使用StreamReader和Writer来保留这些分数的文件连接器,因为我必须为此项目使用StreamReader和Writer。在此先感谢 – Hala7

+0

请参阅[为什么是“有人可以帮我吗?”不是一个实际的问题?](http://meta.stackoverflow.com/q/284236) – EJoshuaS

回答

0

可以读取文件的行列表,然后将其用,而不是使用SteamReader LINQ的 因此,排序,试试这个:

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); 
    } 
+0

我appriciate你的帮助,但请我需要使用StreamReader对于这个项目,如果有人知道如何? – Hala7

+0

再次感谢你,但它又是按升序排列,我可以通过选择排序来实现,但我需要按照降序排列,就像我有分数420,490,120,320,就像是490,420,320, 120,我是begginer所以也许你的代码是好的,但我没有正确使用它或你误会我.. Anyona其他解决方案请吗? – Hala7

+0

a已经编辑了我的答案。在排序后,你用'hiscores.Reverse()'倒序。现在就试试。 – Nino