C# StreamReader文件如何仅在列表框中达到高分时更新?
如何编写我的C# StreamReader文件如何仅在列表框中达到高分时更新?,c#,streamreader,streamwriter,C#,Streamreader,Streamwriter,如何编写我的StreamReader文件,如果它超过当前的高分,它将只覆盖游戏高分?当前,所有分数都将更新到列表框中,而不仅仅是超过当前分数/txt文件的分数 提前谢谢 public partial class Form1 : Form { int Dice; int RunningTotal; int MaxRolls; int RollCount; Random rand = new Random(); public Form1()
StreamReader
文件,如果它超过当前的高分,它将只覆盖游戏高分?当前,所有分数都将更新到列表框中,而不仅仅是超过当前分数/txt文件的分数
提前谢谢
public partial class Form1 : Form
{
int Dice;
int RunningTotal;
int MaxRolls;
int RollCount;
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
RollDiebutton.Enabled = true;
StartOverbutton.Enabled = true;
ClickBeginGametoStartlabel.Visible = false;
int beginTotal = 0;
TotalMoneyEarnedlabel.Text = beginTotal.ToString("c");
MaxRolls = rand.Next(3) + 2;
}
private void button4_Click(object sender, EventArgs e)
{
try
{
string highScore;
StreamReader inputFile;
inputFile = File.OpenText("Highscore.txt");
HighscoreBox.Items.Clear();
while (!inputFile.EndOfStream)
{
highScore = inputFile.ReadLine();
HighscoreBox.Items.Add(highScore);
}
inputFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
RollDiebutton.Enabled = false;
BeginGamebutton.Enabled = true;
StartOverbutton.Enabled = false;
TotalMoneyEarnedlabel.Text = "";
BeginpictureBox.Image = P2Kbembow.Properties.Resources.Begin;
Pressyourluckandrollagainlabel.Visible = false;
ClickBeginGametoStartlabel.Visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
//Close Form
this.Close();
}
private void RollDiebutton_Click(object sender, EventArgs e)
{
Random rand = new Random();
Dice = rand.Next(6) + 1;
RunningTotal += 0;
int DollarAmount = 100;
int Sum = (Dice * DollarAmount);
BeginGamebutton.Enabled = false;
Pressyourluckandrollagainlabel.Visible = true;
RunningTotal += Sum;
TotalMoneyEarnedlabel.Text = RunningTotal.ToString("c");
RollCount += 1;
if (MaxRolls == 0)
{
Random getMax = new Random();
TotalMoneyEarnedlabel.Text = "";
}
else
if (RollCount >= MaxRolls)
{
MaxRolls = 6;
RollCount = 0;
RunningTotal = 0;
TotalMoneyEarnedlabel.Text = "$0.0";
Show(); MessageBox.Show("Sorry! You lose!");
RollDiebutton.Enabled = false;
BeginGamebutton.Enabled = true;
TotalMoneyEarnedlabel.Text = "";
BeginpictureBox.Image = P2Kbembow.Properties.Resources.Begin;
Pressyourluckandrollagainlabel.Visible = false;
ClickBeginGametoStartlabel.Visible = true;
StartOverbutton.Enabled = false;
return;
}
StreamWriter outputFile;
outputFile = File.CreateText("HighScore.txt");
outputFile.WriteLine(TotalMoneyEarnedlabel.Text);
outputFile.Close();
if (Dice == 1)
{
//shows Image of dice 1
BeginpictureBox.Image = P2Kbembow.Properties.Resources._1Die;
}
if (Dice == 2)
{
//shows Image of dice 2
BeginpictureBox.Image = P2Kbembow.Properties.Resources._2Die;
}
if (Dice == 3)
{
//shows Image of dice 3
BeginpictureBox.Image = P2Kbembow.Properties.Resources._3Die;
}
if (Dice == 4)
{
//shows Image of dice 4
BeginpictureBox.Image = P2Kbembow.Properties.Resources._4Die;
}
if (Dice == 5)
{
//shows Image of dice 5
BeginpictureBox.Image = P2Kbembow.Properties.Resources._5Die;
}
if (Dice == 6)
{
//shows Image of dice 6
BeginpictureBox.Image = P2Kbembow.Properties.Resources._6Die;
}
//Display Message Box of dice rolled
Show(); MessageBox.Show(" You rolled a " + Dice + "!");
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
string highScore;
StreamReader inputFile;
inputFile = File.OpenText("Highscore.txt");
HighscoreBox.Items.Clear();
while (!inputFile.EndOfStream)
{
highScore = inputFile.ReadLine();
HighscoreBox.Items.Add(highScore);
}
inputFile.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
代码中有问题的部分非常琐碎。要仅将文件中的最高分数添加到列表框,您可以使用:
var highScore = File.ReadAllLines("Highscore.txt").Max(score => decimal.Parse(score));
HighscoreBox.Items.Clear();
HighscoreBox.Items.Add(highScore);
但你还需要注意其他一些事情:
string _fileName = "Highscore.txt";
private void Form1_Load(object sender, EventArgs e)
{
if (!File.Exists(_fileName))
{
File.Create(_fileName);
return;
}
LoadHighestScore();
}
private void LoadHighestScore()
{
HighscoreBox.Items.Clear();
var highestScore = GetHighestScore();
HighscoreBox.Items.Add(highestScore);
}
private decimal GetHighestScore()
{
var scores = File.ReadAllLines(_fileName);
if (scores.Length == 0)
return 0;
return scores.Max(score => decimal.Parse(score));
}
private void RollDiebutton_Click(object sender, EventArgs e)
{
//-------------------------
//-------------------------
WriteScore(score);
//-------------------------
//-------------------------
}
private void WriteScore(string score)
{
File.AppendAllLines(sd, new string[]{ score });
}
每次都会将分数附加到您的文件中。如果您只需要在分数超过现有最高分数(您的问题不太清楚)的情况下写作,您可以:
private void WriteScore(string score)
{
if (int.Parse(score) > GetHighestScore())
File.AppendAllLines(sd, new string[]{ score });
}
SELECT MAX(score) FROM highscores;
请将大部分代码分解为不同的方法,以帮助代码重用。如果可以将逻辑移到其他类,则效果更好
要做到这一点,最好使用像SQLite这样的小型轻量级客户机数据库。将来,如果您需要更复杂的操作,如“每个用户的最高分数”,该怎么办?要获得最大分数,您可以执行以下操作:
private void WriteScore(string score)
{
if (int.Parse(score) > GetHighestScore())
File.AppendAllLines(sd, new string[]{ score });
}
SELECT MAX(score) FROM highscores;
就这么简单
最后,请发布代码中有问题的部分,而不是一大堆不相关的行。覆盖游戏高我的高分框高分?我无法理解这一点。你的高分标准是什么?你想覆盖文本文件上的高分值吗?对不起,我相信我编辑的地方听起来不像是个白痴哈哈。但是,如果当前的新总分大于表格或我创建的txt文件上显示的上一个高分,我需要游戏将新的总分覆盖到文本文件中,这样它将显示在表格上。Yes Codelgnoto这是正确的,只有高分才会更新到txt文件,并在我的程序中显示在新游戏中。@cpkob我想您只想显示从文本文件到列表框的最高分?目前您正在将每个分数添加到列表框中感谢您的响应,但在我的文件中使用它似乎不起作用,这会使我的程序崩溃。你还有什么建议可以借给我吗?以最简单的形式,只有高分才能反映。很抱歉有这么多代码。我是编程的超级初学者。@cpkob它在哪里崩溃?在哪一行?告诉我们坠机消息。如果没有这些,就不可能告诉你。我只是自己运行了上面的代码,没有看到崩溃。