플레이어 클래스를 만들었고 플레이어를 만들려고하는 메뉴 기반 플레이어 시스템에서 해당 클래스의 배열을 만들었습니다. 내 ProcessCreate 메서드는 InsertPlayer에서 반환 된 삽입 인덱스를 확인하고 -1과 같지 않은 경우 "성공적으로 만든"메시지를 표시하지만 -1을 반환하는 GetInsertIndex 메서드를 얻는 방법을 찾는 데 어려움을 겪고 있습니다. InsertPlayer의 범위 예외는 사용자가 음수를 입력 또는 채팅 여기서 인덱스를 음수로 반환하는 방법
그것으로 확인 내 ProcessCreate 방법의 일부 이해 될 것이다빈 문자열을 가지는 경우 경우 밖으로 인덱스 버리지 -1
playerindex = InsertPlayer(number, firstName, lastName, goals, assists, players, ref playerCount);
if (playerindex != -1 || playerCount > 0)
{
Console.WriteLine("\n{0,7} {1,-20}{2, -20}{3,8}{4,8}{5,8}\n", "Number", "First Name", "Last Name", "Goals", " Assists", "Points");
Console.WriteLine("{0,7} {1,-20}{2, -20}{3,8}{4,8}{5,8}",
players[playerindex].Number, players[playerindex].FirstName, players[playerindex].LastName,
players[playerindex].Goals, players[playerindex].Assists, players[playerindex].Points());
Console.WriteLine("Sucessfully created!");
Console.WriteLine();
}
}
else
Console.WriteLine("\nCreate Player: the player number already exists");
}
else
Console.WriteLine("\nCreate Player: the player roster is already full");
}
당신은 당신이 말한대로 -1 인덱스를 반환에 대해 -1 그나마 걱정을 반환하려면 여기 내 InsertPlayer 및 getInsertIndex를 방법
static Int32 InsertPlayer(Int32 number, String firstName, String lastName, Int32 goals,
Int32 assists, Player[] players, ref Int32 playerCount)
{
Int32 insertIndex, shiftCount;
insertIndex = GetInsertIndex(number, players, ref playerCount);
for (shiftCount = playerCount; shiftCount > insertIndex; shiftCount--)
players[shiftCount] = players[shiftCount - 1];
try
{
players[insertIndex] = new Player(number, firstName, lastName, goals, assists);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
if (insertIndex != -1)
{
playerCount++;
}
return insertIndex;
}
static Int32 GetInsertIndex(Int32 number, Player[] players,
ref Int32 playerCount)
{
Int32 index = 0;
bool found = false;
while (index < playerCount && found == false)
if (players[index].Number > number || playerCount <= 0)
{
found = true;
index = -1;
}
else
index++;
return index;
}
음수 값을 사용하여 배열을 색인 할 수 없으므로 수행하려는 작업이나 오류를 일으키는 행이 확실하지 않습니다. – OldProgrammer
모든 메서드에 중단 점을 배치하고 코드를 단계별로 실행합니다. 무엇이 잘못되었는지에 대한 자세한 내용을 알아낼 수 있으며 여기에서 게시 할 수 있습니다. –
나는이 문제가이 줄로 생각한다. if (playerindex! = -1 || playerCount> 0) 아마도 || 대신에 &&를 원할 것이다. – krolth