2015-02-10 122 views
-2

我试图按照从高到低的顺序排列我的排行榜的分数。 我不知道我会如何做到这一点,我知道这不是一个地方要求写的代码,但我已经到处寻找,我需要创建一个数组或列表,或一个类?高分排行榜

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

     } 
    } 
} 

当前分数在输入时保存。

+0

将结果保存在数组或List中,然后可以使用LINQ和OrderByDescending方法。 – 2015-02-10 18:37:48

回答

1

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

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

或者,让他们降:

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

,让你的分数排序的名单。然后使用收集视图(如ListBox),可以在窗体上显示列表。