2014-11-02 3 views
0

안녕하세요 저는 성을위한 3 개의 배열을 보유하고있는 프로그램을 만들고 있습니다. 하나는 점수를 매기고 다른 하나는 플레이어 수를 나타냅니다. 이제는 모든 배열과 모든 것을 끝내지 만 확실하지는 않습니다. 어떻게 여러 배열에 대한 항목을 삭제할 것인지에 대해 설명합니다. 지금까지이게 내가 delete player 메소드에 대해 가지고있는 것입니다. 올바른 방향으로 인도하면 도움이되고 감사 할 것입니다.배열 여러 배열에서 항목을 삭제하는 방법?

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

어쩌면 배열은 당신의 사건에 대한 올바른 형식이 아닙니다. –

+1

배열을이 용도로 사용하지 마십시오. 플레이어의 관련 데이터를 나타내는 클래스를 정의하십시오. –

+4

'Player' 클래스는 관련 정보를 유지하고 List 은 여러 플레이어를 더 쉽게 관리 할 수 ​​있습니다. – Plutonix

답변

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); 
    } 
} 
관련 문제