如何将索引返回为负数
本文关键字:返回 索引 | 更新日期: 2023-09-27 18:32:30
我创建了一个播放器类,并从该类为我的菜单驱动的播放器系统制作了一个数组,我正在尝试创建一个播放器。我的ProcessCreate方法假设检查从InsertPlayer返回的插入索引,如果它不等于-1,则显示"成功创建"消息,但是我很难弄清楚如何让我的GetInsertIndex方法返回-1,并且当用户输入负数或具有空字符串时,如果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");
}
这是我的 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;
}
您应该非常考虑将玩家数组更改为使用List<Player>
,因为列表提供了许多您需要的功能。
如果像这样重写InsertPlayer
,则可以消除GetInsertIndex
:
static int InsertPlayer(int number, string firstName, string lastName,
int goals, int assists, List<Player> players)
{
var index = -1;
if (!players.Any(p => p.Number == number))
{
var player = new Player(number, firstName, lastName, goals, assists);
index = players.FindLastIndex(x => x.Number < number) + 1;
players.Insert(index, player);
}
return index;
}
如果列表中已存在玩家编号,则此代码返回-1
,否则它将在正确的位置添加新玩家,然后返回新索引。
请避免使用catch (Exception e)
- 这只会使调试变得非常困难,并且不会真正改善您的代码。
另外,避免使用瘟疫等ref
。它们很少需要。您传递它的事实表明您已经预先分配了一个大型players
数组,以避免调整其大小。好吧,List<Player>
为您处理所有大小调整,并允许您随时使用players.Count
。
没有阅读您的整个代码,但是由于您的玩家列表在实际数组中,您不能执行以下操作:
Player player = new Player();
try
{
player = new Player(number, firstName, lastName, goals, assists);
//or assign individually instead of using the constructor
//if your player class allows it
players[insertIndex] = player;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
然后返回:
return Array.IndexOf(players,player);
如果它在那里,它将返回索引,如果它不存在,它将返回 -1
如果你想返回一个 -1,不要担心像你说的那样返回一个 -1 索引。 只需返回 -1; 在你的例外中。
catch(Exception e)
{
Console.WriteLine(e.Message);
insertIndex = -1;
// or
return -1;
}
虽然我建议检查字符串是否为空并自己捕获错误并返回 -1;