无法将int []类型隐式转换为int

时间:2014-11-14 03:29:56

标签: c#

我创建了一个播放器类,我正在尝试使用我的播放器类删除我的菜单驱动播放器程序的播放器,但我不断收到错误:无法使用我的ProcessDelete方法隐式转换int []类型为int'。我的ProcessDelete方法假设在用户输入玩家号码时删除玩家的所有数据。

任何帮助都将不胜感激。

     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");
        }

    }
}

4 个答案:

答案 0 :(得分:1)

您的ProcessDelete方法返回一个数组,但其签名指定它应返回int

您还没有对返回值做任何事情,所以只需将签名更改为void

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

从该方法的末尾删除return行:

return newIndicesArray;  // remove this

FWIW,您可以考虑使用List<Player>集合而不是Player[]数组。

获得索引后,您可以使用RemoveAt()轻松删除所需的元素,整个ProcessDelete()方法可能会消失。

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

players.RemoveAt(indexToRemove);

答案 1 :(得分:0)

在您的ProcessDelete方法中,您返回一个整数数组,但您的方法签名表明它返回一个int。我猜你想要归还我或者其中一个吗?

答案 2 :(得分:0)

不允许将Int32[]转换为Int32,请尝试以下操作:

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;
    }

答案 3 :(得分:0)

您应该更改函数的数据类型,因为它返回一个整数数组,因此它必须具有以下签名:

static Int32[] ProcessDelete(...)