【发布时间】:2015-04-13 19:26:22
【问题描述】:
我正在尝试在richTextBox 中按从高到低的顺序排列测验的分数。我将球员姓名、测验和分数保存在一个名为scoresClass 的类中。在测验结束时,我给全班三个richTextBoxs 打电话,显示他们的名字、测验和分数。然后我将它们添加到列表中并将列表写入文件。排行榜为richTextBox,设置为等于文件中的数据。
这是我的代码:
public frmFinal()
{
InitializeComponent();
}
private void frmFinal_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
List<string> scores = new List<string>();
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);
rchBxNameScore.Text = scoresClass.name;
rchBxQuizNameScore.Text = scoresClass.quiz;
}
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");
scores.Clear();
}
//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))
{
scores.Add(scoresClass.name + "\t" + scoresClass.quiz + "\t" + scoresClass.score);
foreach(string score in scores)
{
scoreInfo.WriteLine(score);
}
}
//clears all the players score details
rchBxNameScore.Clear();
rchBxQuizNameScore.Clear();
rchBxScore.Clear();
rchBxScores.Text = File.ReadAllText(".\\scoreInfo.bin");
//sets saved to true
saved = true;
}
}
目前分数是按输入时间计算的,而不是按分数计算的。我不确定我会如何实际订购它们。
这是课程:
public class scoresClass
{
public static int score = 0;
public static string name = "";
public static string quiz = "";
public scoresClass(string userName, int userScore, string userQuiz)
{
name = userName;
score = userScore;
quiz = userQuiz;
}
}
【问题讨论】:
-
我喜欢这个评论:
//creates a bool variable and sets it equal to false严肃地说,scoresClass是什么,它在哪里定义/填充? -
@MichaelMcGriff 它是一个类,它包含玩家的姓名、测验名称和分数。
-
你能贴出显示
scoresClass的定义和人口的代码吗? -
@MichaelMcGriff 刚刚添加
-
您使用的是
List<scoresClass>()吗?我找不到您在遍历分数集合的任何地方。我看到你使用scoresClass的唯一地方你把它当作单身人士对待。
标签: c# string list class leaderboard