2017-03-02 38 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

謝謝。請參閱編輯的版本 –

相關問題