首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将秒表时间保存到文本文件

将秒表时间保存到文本文件
EN

Stack Overflow用户
提问于 2014-10-20 13:49:26
回答 2查看 963关注 0票数 0

我正在创建一个内存游戏(匹配游戏),并且我试图节省玩家在匹配所有瓷砖时获得的时间。我的编曲很有用,我很好奇为什么。下面是保存方法的代码和查找是否所有瓷砖匹配的方法:

代码语言:javascript
运行
复制
    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.
        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-20 14:03:14

在调用time时,您似乎没有指定参数Save。将timeLabel.Text添加到函数调用中。

编辑:使用StreamWriters的一个良好实践是使用可用的using命令。由于StreamWriter是可处理的,所以可以将它的用法包装在using中,而不必担心关闭它。请参阅更新后的Save函数。

代码语言:javascript
运行
复制
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.
        }
票数 1
EN

Stack Overflow用户

发布于 2014-10-20 14:02:24

保存应该如下所示:

代码语言:javascript
运行
复制
private void Save(string time)
{
   File.WriteAllText(path, time); // Or AppendAllText, depends on what you want.
}

在CheckForWinner内部,您必须调用的不是Save()而是Save(timeLabel.Text)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26467282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档