如何读取字符串文件并转换为int

时间:2015-06-12 22:33:21

标签: c# list

我是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);
}

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

编辑更多信息:抱歉缺少信息,但是highscores是一个列表。每次玩家获得新的高分时,都会创建一个新的条目,并保持最大值为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>();

                }

如果还有其他任何我可以添加澄清请说出来。

2 个答案:

答案 0 :(得分:0)

尝试使用linq

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

.Select() clause需要predicatedelegate function returnsIEnumerable<T>,并使用lambda operator为您的起始variable中的每个iterator分配一个IEnumerable<T>,在本例中为intList,每个iterator我指定为item。在lambda operator之后,无论你想要什么,你几乎可以使用它,而.Select() clause会根据你的意愿为你定制IEnumarable<T>

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

答案 1 :(得分:0)

如果我理解你的问题,这就是反向行动

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