我正在创建一个内存游戏(匹配游戏),并且我试图节省玩家在匹配所有瓷砖时获得的时间。我的编曲很有用,我很好奇为什么。下面是保存方法的代码和查找是否所有瓷砖匹配的方法:
private void Save(string time)
{
StreamWriter write = new StreamWriter(path, true);
write.WriteLine(time);
write.Close();
}
private void CheckForWinner()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLabel = control as Label;
{
if(iconLabel != null)
{
if (iconLabel.ForeColor == iconLabel.BackColor)
{
return;
}
}
}
}
MessageBox.Show("You finished the game, your time was: " + timeLabel.Text);
Save();
//Close(); is outcommented because I want to see if it works.
}
发布于 2014-10-20 14:03:14
在调用time
时,您似乎没有指定参数Save
。将timeLabel.Text
添加到函数调用中。
编辑:使用StreamWriter
s的一个良好实践是使用可用的using
命令。由于StreamWriter
是可处理的,所以可以将它的用法包装在using
中,而不必担心关闭它。请参阅更新后的Save函数。
private void Save(string time)
{
using(StreamWriter write = new StreamWriter(path, true)){
write.WriteLine(time);
}
}
private void CheckForWinner()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLabel = control as Label;
{
if(iconLabel != null)
{
if (iconLabel.ForeColor == iconLabel.BackColor)
{
return;
}
}
}
}
MessageBox.Show("You finished the game, your time was: " + timeLabel.Text);
Save(timeLabel.Text);
//Close(); is outcommented because I want to see if it works.
}
发布于 2014-10-20 14:02:24
保存应该如下所示:
private void Save(string time)
{
File.WriteAllText(path, time); // Or AppendAllText, depends on what you want.
}
在CheckForWinner内部,您必须调用的不是Save()而是Save(timeLabel.Text)。
https://stackoverflow.com/questions/26467282
复制相似问题