2017-02-25 82 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