假设我有一个文件,它包含以下内容:如何从文件中读取某些数据并将其显示出来?
Game Name: Candy Crush
Game Type: Match Three
Game Difficulty Rating: 8
Game Name: Maplestory
Game Type: RPG
Game Difficulty: 6
Game Name: Runescape
Game Type: RPG
Game Difficulty: 6
Game Name: GTA
Game Type: Video
Game Difficulty: 7
如何发布仅一个消息框RPG游戏?我想显示列出的每个RPG游戏的所有数据。
我刚才试过这段代码,但它似乎是显示所有它。
private void button3_Click(object sender, EventArgs e)
{
var file = File.ReadAllText("Games.txt");
var enter = Environment.NewLine;
var gamestrings = games.Select(x => string.Format("{0}\r\n{1}\r\n{2}", x.Name, x.Type, x.Difficulty));
MessageBox.Show(string.Join(enter + enter, gamestrings));
var games = file.Split(new[] { enter + enter }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Split(new[] { enter }, StringSplitOptions.RemoveEmptyEntries)).Select(e => new { Name = e[0], Type = e[1], Difficulty = e[2] }).Where(x => x.Type.Contains("RPG"));
}
给我的错误:
Cannot use local variable 'games' before it is declared
A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else
我建议你仔细阅读[这篇文章](http://mattgemmell.com/2008/12/08/what-have-you-tried/) –
@HighCore如果我只是尝试阅读整个文件而不是? – puretppc
然后您需要在问题中发布当前代码,并指定您遇到问题的代码的哪些部分,以及哪些/哪些不工作以及您的预期结果如何。 –