2014-11-03 2 views
-1

나는 사람의성에 대해 하나, 점수를받은 것에 대해 하나, 플레이어 번호에 하나씩 세 개의 배열을 포함하는 프로그램을 만들고 있습니다. 이제 필자는 모든 배열을 가지고 모든 것이 다하지만 난 내 ProcessDelete 메서드를 호출 할 때 나는 점점 계속계속 가져 오기 IndexOutOfRange

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

System.IndexOutOfRangeException

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

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

      index++; 
     } 

     while (index2 < playerLastName.Length) 
     { 
      if (index2 != playerCount) 
      { 
       newArray2[k] = playerLastName[index2]; 
       k++; 
      } 

      index2++; 
     } 
      while (index3 < playerLastName.Length) 
     { 
      if (index3 != playerCount) 
      { 
       newArray3[t] = playerPoints[index3]; 
       t++; 
      } 

      index3++; 
     } 
     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(); 
        ProcessDelete(playerNumbers, ref playerCount, playerLastName, playerPoints); 
       } 
      } 
      else 
       Console.WriteLine("\nDelete Player: player not found"); 
     } 
     else 
      Console.WriteLine("\nDelete Player: the roster is empty"); 
    } 

} 

}

+0

어제 아주 비슷한 질문을하지 않았습니까? –

+2

예외에 대한 스택 추적을 보면 예외 발생 위치를 정확히 알려줍니다. 디버거에서 코드를 실행하면 디버거에서 예외가 발생하는 위치를 표시하지 않고 범위를 벗어난 변수를 실제로 볼 수도 있습니다. 예외가 어디서 어떻게 발생하는지 정확히 알게되면 문제를 해결하는 방법을 알지 못하면 문제를 구체적인 문제로 업데이트하고 정확한 문제를 설명 할 수 있습니다. –

+0

'ProcessDelete'에 대한 호출을 주석 처리하면 응용 프로그램이 오류를 던지지 않습니까? 또한'player'와'playerindex'의 어떤 값이 생성됩니까? –

답변

0

세 번째 루프에서 다음을 얻었습니다.

newArray3[t] = playerPoints[index2]; 
k++; 

다음과 같아야합니다.

newArray3[t] = playerPoints[index3]; 
t++; 
+0

동일한 위치에서 index2가 사용되고 있으며 index3이어야합니다. 아마도 하나의 루프 만 있어야 할 것입니다. –

+0

@BenVoigt : 맞습니다.'index2'를 사용하면 오류 메시지의 원인 일 수 있습니다.'t' 대신'k'를 늘리면 마지막 항목을 제외한 모든 항목을 버릴 수 있습니다. 세 배열에 대해 하나의 루프를 사용하면 더 좋을지 모르지만 세 속성을 포함하는 한 배열의 개체가 가장 좋습니다. – Guffa

관련 문제