假設我有一個文件,它包含以下內容:如何從文件中讀取某些數據並將其顯示出來?
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
然後您需要在問題中發佈當前代碼,並指定您遇到問題的代碼的哪些部分,以及哪些/哪些不工作以及您的預期結果如何。 –