2017-03-02 48 views
1

所以我一直使用C#创建一个测验显示(只是为了好玩和学习) 在我的高分电网从playerdetails类的信息对同一个玩家显示5次而不是一次。见代码:写入一个文本文件在C#中,但是当它在网格中的相同信息的重复是

public frmHighscore() 
    { 
     InitializeComponent(); 

     //Only save the test scores once 
     if (!File.Exists("highscores.txt")) 
     { 
      SaveScores(); 
     } 

     LoadScores(); 
     //Sort the grid based on the value of Column 1 which will be the score value 
     dataGridView1.Sort(dataGridView1.Columns[1], ListSortDirection.Descending); 
    } 

    private void SaveScores() 
    { 
     //Open filestram and streamwriter needed to save details to a text file 
     FileStream fileStream = new FileStream("highscores.txt", FileMode.Append, FileAccess.Write); 
     StreamWriter streamWriter = new StreamWriter(fileStream); 

     try 
     { 
      //loop over each player 

      { 
       //Write details of the player to the textfile in format playerName~Score 
       foreach (Player player in PlayerList) 
       { 
        streamWriter.WriteLine(player.playerName + "~" + player.playerScore); 
       } 
      } 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("Error loading the scores", "Please try again"); 
     } 
     finally 
     { 
      //close streamwriter and filestream 
      streamWriter.Close(); 
      fileStream.Close(); 
     } 
    } 

    private void LoadScores() 
    { 
     //Check if the file exists 
     if (File.Exists("highscores.txt")) 
     { 
      //Read in all the details of the text file 
      var playerScores = File.ReadAllLines("highscores.txt"); 

      //check if players exist in the text file 
      if (playerScores.Length > 0) 
      { 
       //Loop over each player details 
       foreach (var playerScore in playerScores) 
       { 
        //Add the player name and score to the datagrid 
        var splitDetails = playerScore.Split('~'); 
        dataGridView1.Rows.Add(splitDetails[0], Convert.ToInt32(splitDetails[1])); 
       } 
      } 
      else 
      { 
       //Hide the grid and show the No Scores label 
       HideGrid(); 
      } 
     } 

我Player类看起来是这样的: 命名空间CWQuiz

{ 
     public class Player 
{ 
    public string Username; 
    public int Score; 
    public int quizNumber; 

    public string playerName { get; set; } 
    public int playerScore { get; set; } 

    public static List<Player> player = new List<Player>(); 


    public Player(string name) 
    { 
     Username = name; 
     Score = 0; 
    } 

} 
} 
+0

foreach(PlayerDetails中的var播放器) streamWriter.WriteLine(player.playerName +“〜”+ player.playerScore); } –

+0

'PlayerDetails.playerName'是什么类型? –

+0

这是一个字符串类型 –

回答

0

如果player变量是什么,我认为它是那么

streamWriter.WriteLine(PlayerDetails.playerName + "~" + PlayerDetails.playerScore); 

应该

streamWriter.WriteLine(player.playerName + "~" + player.playerScore); 
+1

根据OP的评论'PlayerDetails.playerName'是一个'string'因此'player'将会是'char',编译器会抱怨说它找不到'playerName'属性。 –

0

如果你说PlayerDetails.playerNamestring类型,那么您通过string其具有6个字符循环,则在打印每次相同的信息:

PlayerDetails.playerName + "~" + PlayerDetails.playerScore =>名〜6

这就是为什么你得到6行。

如果你只需要打印当前玩家的名字和分数那么就使用它没有循环:

streamWriter.WriteLine(PlayerDetails.playerName + "~" + PlayerDetails.playerScore); 

如果你想有多个玩家在游戏中 我会建议使用适当的结构来保存玩家的信息。有一个包含所有的信息(你可能已经)

public class Player 
{ 
    public string playerName { get; set; } 
    public int playerScore { get; set; } 
} 

和像List<Player>一个集合,其中包含所有玩家一类Player。然后,您将循环播放玩家列表并使用迭代变量player访问每位玩家的属性。

List<Player> PlayerList = new List<Player>(); 

PlayerList.Add(new Player(){playerName = "Alfred", playerScore = 0}); 

// start the game 
//... 

// write down the results of all players 
foreach (Player player in PlayerList) 
{ 
    streamWriter.WriteLine(player.playerName + "~" + player.playerScore); 
} 

当通过收集用foreach循环再元素item

foreach (var item in collection) 
      ^
       | 

表示每个项目,并允许您访问其属性。在你的情况下,collectionstring,它由charsitem组成,代表该字符串中的每个字符。

编辑

1)的问题是,您添加的值到类:

public string playerName { get; set; } 
public int playerScore { get; set; } 

但是你从来没有给它们赋值。在构造函数中,你仍然只初始化:

Username = name; 
Score = 0; 

所以你也应该使用这些值。 UsernameScore在for循环中。

2)接下来的问题是PlayerList不属于类Player。它不是玩家的财产,它是游戏的财产,所以它应该是游戏发生的地方。因为在那里你也可以将玩家添加到想要玩的列表中。把它想象成一个真实的世界场景。在类frmHighscore要通过PlayerList

foreach (Player player in PlayerList) 
{ 
    streamWriter.WriteLine(player.playerName + "~" + player.playerScore); 
} 

循环,但我没有看到这个变量存在的任何声明。编译器应该抱怨这个,它找不到它。你需要在那里添加。也可以在这个课程中填写它,或者通过初始化玩家并将其添加到列表中的课程参考。

我希望你现在能更好地理解它。

+0

啊是的,我现在可以看到,无论我把用户名放在这里,都是我得到的线条。然而,我已经推荐了代码,但是对于PlayerList来说,我不想手动添加这些代码,它也会抛出一个错误,说它是一个字段,但是用作一个类型 –

+0

将玩家添加到列表中的要点仅限于插图。至于错误,由于我没有看到你的代码,所以我无法帮助你。您可以使用您的文章下方的[编辑按钮](http://stackoverflow.com/posts/42552505/edit)编辑,添加和更新此信息到您的问题 –

+0

谢谢。请参阅编辑的版本 –

相关问题