2014-11-14 9 views
-1

플레이어 클래스를 만들었고 플레이어 클래스를 사용하여 플레이어 기반 프로그램에서 플레이어를 삭제하려고하지만 오류가 계속 발생합니다. 암시 적으로 int [] 유형을 변환 할 수 없습니다. ~ int '내 ProcessDelete 메서드. 내 ProcessDelete 메서드는 사용자가 플레이어 번호를 입력 할 때 플레이어의 모든 데이터를 삭제한다고 가정합니다.암시 적으로 int [int] 형식을 int로 변환 할 수 없습니다.

도움을 주시면 감사하겠습니다.

 static Int32 ProcessDelete(Player[] players, Int32 RemoveAt) 
     { 

      Int32[] newIndicesArray = new Int32[players.Length - 1]; 

      int i = 0; 
      int j = 0; 
      while (i < players.Length) 
      { 
       if (i != RemoveAt) 
       { 
        newIndicesArray[j] = players[i]; 
        j++; 
       } 

       i++; 
      } 

      return newIndicesArray; 
     } 


     static void DeletePlayer(Int32 number, String firstName, String lastName, Int32 goals, 
      Int32 assists, Player[] players, 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(number, firstName, lastName, goals, assists, players, ref playerCount); 


       if (playerindex != -1) 
       { 

        { 

         Console.WriteLine("\nDelete Player: Number - {0}, Name - {1}, Points - {2}", players[playerindex].Number, firstName[playerindex], lastName[playerindex], players[playerindex].Goals, players[playerindex].Assists); 
         Console.WriteLine("Succesfully Deleted"); 
         Console.WriteLine(); 
         ProcessDelete(players); 
        } 
       } 
       else 
        Console.WriteLine("\nDelete Player: player not found"); 
      } 
      else 
       Console.WriteLine("\nDelete Player: the roster is empty"); 
     } 

    } 
} 
+0

오류의 어느 부분을 이해하지 못합니까? – SLaks

답변

1

당신의 ProcessDelete 방법은 배열을 반환하지만 그 서명은 그것이 int을 반환해야 함을 지정합니다.

당신은 그래서 그냥 void에 서명을 변경, 어쨌든 반환 값으로 아무것도하지 않는

:

static void ProcessDelete(Player[] players, Int32 RemoveAt) 

을 그리고 그 방법의 끝에서 return 줄을 제거 :

return newIndicesArray; // remove this 

FWIW 인 경우 Player[] 배열 대신 List<Player> 컬렉션을 사용하는 것이 좋습니다.

인덱스가 있으면 RemoveAt()을 사용하여 필요한 요소를 쉽게 제거 할 수 있으며 전체 ProcessDelete() 메서드는 그냥 사라질 수 있습니다.

List<Player> players = new List<Player>; 
... 
// some code to add players 
... 
int indexToRemove = GetPlayerIndex(...); 

players.RemoveAt(indexToRemove); 
+1

그게 그의 문제예요 +1. 그가 반환 값을 원했던 것처럼 보입니다 만, 모든 것이 너무 복잡하기 때문에 이해할 수 없습니다. – Jonesopolis

+1

@Jonesy 동의 했으므로 따르기가 어렵습니다. 나는 그가'Player []'로하려고하는 것을하기보다는'List '을 조작하는 것이 훨씬 낫다고 생각한다. –

0

ProcessDelete 메서드에서 정수 배열을 반환하지만 메서드 서명이 단일 int를 반환한다고 말합니다. 내가 너 나 나 중 하나를 돌려 주겠다고 생각하는거야?

0

Int32-Int32[] 노력이 허용되지 않습니다 Converting이 :

static Int32[] ProcessDelete(Player[] players, Int32 RemoveAt) //Change Int32 to In32[] 
    { 
     Int32[] newIndicesArray = new Int32[players.Length - 1]; 

     int i = 0; 
     int j = 0; 
     while (i < players.Length) 
     { 
      if (i != RemoveAt) 
      { 
       newIndicesArray[j] = players[i]; 
       j++; 
      } 

      i++; 
     } 

     return newIndicesArray; 
    } 
0

귀하는 정수의 배열을 반환하기 때문에 아래와 같이 서명이 있어야하므로, 함수의 데이터 유형을 변경해야합니다

static Int32[] ProcessDelete(...) 
관련 문제