【问题标题】:How to read a string file and convert to int [duplicate]如何读取字符串文件并转换为 int [重复]
【发布时间】:2015-06-12 22:33:21
【问题描述】:

我是 C# 的新手,我一直在寻找解决这个问题的方法,尽管我尝试的任何方法似乎都不起作用:(

public Form1()         
{ 
    InitializeComponent();        
    File.ReadAllLines(@"C:\Users\Simon\test.txt");
    ???
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
{
    List<string> newlist = highscores.ConvertAll<string>(x => x.ToString());
    File.WriteAllLines(@"C:\Users\Simon\test.txt", newlist);
}

这是我现在的代码,如您所见,我通过将整数转换为字符串并写入它们,成功地将高分(列表)写入文件。现在我必须阅读它们并将它们转换回整数(稍后在我的代码中,玩家通过创建新的高分来添加整数)。我尝试使用 convert 方法,但我所做的一切似乎都不起作用,我不确定我是否应该在这里使用数组,但考虑到我只使用了列表,这会让我感到惊讶。正如我所说,我是编程新手,所以我可以在这方面使用一些帮助:p

编辑更多信息:抱歉信息不足,但高分是一个列表。每次玩家获得新的高分时,都会创建一个新条目,并且最多保持 5 分。这就是我试图进入文本文件的内容,以便在程序运行时可以“保存”和检索高分又开始了。程序启动时,我无法将高分重新添加到列表中。

更多代码: 全局变量

List<int> highscores = new List<int>(); 
int totaltime = 0;
int timeleft = 3;

列表的工作原理和内容。游戏的目的是躲避尽可能多的方格,如果它相交会发生以下情况。

if (picturebox.Bounds.IntersectsWith(pictureBox1.Bounds))
                {
                    pictureBox1.Location = new Point(668, 580);
                    pictureBox2.Location = new Point(622, 30);
                    timer4.Stop();
                    timer3.Stop();
                    timer2.Stop();
                    timer1.Stop();
                    timer4.Interval = 1000;
                    label2.Hide();
                    button1.Enabled = true;
                    button2.Enabled = true;
                    MessageBox.Show("You died! You lasted " + totaltime + " seconds.");

                    if (highscores.Count < 5)
                    {
                        highscores.Add(totaltime);
                        highscores.Sort();
                    }
                    else
                    {
                        int newscore = totaltime;
                        int lowestscore = int.MaxValue;
                        foreach (int score in highscores)
                        {
                            if (score < lowestscore)
                                lowestscore = score;
                        }
                        if (newscore > lowestscore)
                        {
                            highscores.Remove(lowestscore);
                            highscores.Add(newscore);
                            highscores.Sort();
                        }
                    }
                    textBox1.Clear();
                    for (int n = highscores.Count - 1; n >= 0; n--)
                        textBox1.AppendText(highscores[n].ToString() + "\n");

                    totaltime = 0;
                    timeleft = 3;
                    label5.Text = "0";

                    foreach (PictureBox pic in pbspawn)
                    {
                        if (pic != pictureBox2)
                            this.Controls.Remove(pic);
                    }
                    pbspawn = new List<PictureBox>();

                }

如果还有什么我可以补充说明的,请说出来。

【问题讨论】:

  • 查看序列化程序。 NewtonSoft.Json,或Xml.XmlSerializer
  • 欢迎来到 Stack Overflow!有一本关于 C# 编程语言的完整手册。在询问有关语言语法的基本问题之前,您应该先查看它。见C# Programming Guide。详细参考请见C# Reference
  • 请学习包含重现问题所需的所有信息。您甚至没有告诉我们highscores 的数据类型,更不用说向我们展示它是如何定义或填充的。

标签: c# list


【解决方案1】:

如果我理解正确你的问题,这是相反的操作

var lines = File.ReadAllLines(@"C:\Users\Simon\test.txt");
var highscores = lines.Select(x => int.Parse(x));

【讨论】:

    【解决方案2】:

    尝试使用 linq

    确保在脚本顶部添加using System.Linq;

    .Select() clause 采用 predicatedelegate 代表 function 其中 returns 你是 IEnumerable&lt;T&gt;),并通过使用 lambda operator,分配 @ 987654330@ 用于您的起始IEnumerable&lt;T&gt; 中的每个iterator,在这种情况下是intList,每个iterator 我分配为item。在lambda operator 之后,您几乎可以随心所欲地使用它,.Select() clause 将按照您的意愿为您定制一个IEnumarable&lt;T&gt;

    IEnumerable<int> intList = newlist.Select(item => Int.Parse(item));
    

    【讨论】:

    • 能否请您 edit 解释为什么这段代码回答了这个问题?纯代码答案是discouraged,因为它们不教授解决方案。 (该帖子至少被一位用户标记,大概是因为他们认为应该删除没有解释的答案。)
    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 2015-06-02
    • 2014-05-24
    • 1970-01-01
    • 2019-04-22
    • 2015-06-18
    • 2017-05-07
    • 1970-01-01
    相关资源
    最近更新 更多