如何将索引作为负数返回

时间:2014-12-01 03:05:15

标签: c#

我创建了一个玩家类,并为我的菜单驱动的玩家系统制作了一个数组,我试图创建一个玩家。我的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;
}

3 个答案:

答案 0 :(得分:1)

你应该考虑将玩家阵列更改为使用List<Player>,因为列表提供了许多你需要的功能。

如果您像这样重写GetInsertIndex,则可以删除InsertPlayer

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

答案 1 :(得分:0)

没有读过您的整个代码,但由于您的玩家列表是实际数组,因此您不能执行以下操作:

            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

答案 2 :(得分:-1)

如果你想返回-1,不要担心如你所说的那样返回-1索引。只返回-1;在你的例外内

catch(Exception e)
{
    Console.WriteLine(e.Message);
    insertIndex = -1;
    // or
    return -1;    
}

虽然我建议检查字符串是否为空并自己捕获错误并返回-1;