2014-11-02 75 views
0

您好我正在創建一個程序,該程序包含三個數組,一個用於姓氏,一個用於分數得分和一個用於玩家號碼,現在我已經獲得了所有陣列並完成了所有事情,但我不確定我將如何刪除多個數組的條目。到目前爲止,這是我的刪除播放器方法。一些正確的方向的指導將真正幫助請和謝謝數組如何刪除多個數組中的條目?

static Int32[] ProcessDelete(Int32[] playerNumbers, String[] playerLastName, Int32[] playerPoints, ref Int32 playerCount) 
     { 
      Int32[] newArray = new int[playerNumbers.Length - 1]; 

      int index = 0; 
      int j = 0; 
      while (index < playerNumbers.Length) 
      { 
       if (index != playerCount) 
       { 
        newArray[j] = playerNumbers[index]; 
        j++; 
       } 

       index++; 
      } 

      return newArray; 

     } 

     static void DeletePlayer(Int32[] playerNumbers, String[] playerLastName, Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS) 
     { 
      int player;// Player number to delete 
      int playerindex;//index of the player number in Array 
      if (playerCount < MAXPLAYERS) 
      { 

       player = GetPositiveInteger("\nDelete Player: please enter the player's number"); 
       playerindex = GetPlayerIndex(player, playerNumbers, playerCount); 

       if (playerindex != -1) 
       { 

        {  

         Console.WriteLine("\nDelete Player: Number - {0}, Name - {1}, Points - {2}", playerNumbers[playerindex], playerLastName[playerindex], playerPoints[playerindex]); 
         Console.WriteLine("Succesfully Deleted"); 
         Console.WriteLine(); 

        } 
       } 
       else 
        Console.WriteLine("\nDelete Player: player not found"); 
      } 
      else 
       Console.WriteLine("\nDelete Player: the roster is empty"); 
     } 

    } 
} 
+2

也許陣列是不是你的情況下正確的類型。 – 2014-11-02 16:50:08

+1

不要爲此使用數組,請定義一個表達玩家相關數據的類。 – 2014-11-02 16:50:47

+4

'玩家'類可以將相關信息保存在一起,並且列表可以更容易地管理多個玩家 – Plutonix 2014-11-02 16:51:19

回答

1

是否有任何理由一個簡單的面向對象的方法不會在你的情況下工作?

public class Player 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public int Number { get; set; } 
    public int Points { get; set; } 
} 

public class TeamManager 
{ 
    List<Player> players; 

    public TeamManager() 
    { 
     this.players = new List<Player>(); 
    } 

    public void Add(Player player) 
    { 
     this.players.Add(player); 
    } 

    public bool Delete(Player player) 
    { 
     if (this.players.Contains(player)) 
      return this.players.Remove(player); 
     else 
      return false; 
    } 
} 

正如對問題的評論。你可以有一個簡單的數據結構來保存玩家的數據和更多用戶友好的集合來管理一組玩家。

然後你可以使用它像:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var player1 = new Player 
     { 
      Id = 1, 
      Name = "Mike", 
      Number = 13, 
      Points = 5, 
     }; 

     var team = new TeamManager(); 
     team.Add(player1); 
     team.Delete(player1); 
    } 
} 
相關問題