c# - 删除方法不删除

标签 c#

我正在创建一个包含三个数组的程序:一个用于此人的姓氏,一个用于得分,一个用于球员编号。现在,我得到了所有数组并完成了所有操作,但由于某种原因我的进程被删除了方法不会从数组中删除项目。我知道我问了一个与此类似的问题,但我似乎无法弄清楚为什么它不能正确删除

如有任何帮助,我们将不胜感激,谢谢

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

最佳答案

ProcessDelete方法构造新数组 newArray , newArray2 , 和 newArray3 .其中,它仅返回 newArray , 所以 newArray2newArray3被扔掉。当DeletePlayer电话 ProcessDelete , 它会忽略返回值,所以 newArray也被扔掉了,所有的工作都在ProcessDelete的体内进行被浪费了。

关于c# - 删除方法不删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26707399/

相关文章:

c# - 密封类中的 protected 成员

c# - 从内部类获取 DisplayNameAttribute

c# - 如何从 .NET Core 2.1/2.2 创建 Windows 服务

c# - Blazor - app.UseIdentityServer();使用 .pfx key 文件 - 解析数字时遇到意外字符

c# - 如何使用 nohup 在 Mono 中执行进程?

c# - 新鲜的 ASP.NET Core API 返回空的 JSON 对象

javascript - Javascript 和 C# 之间来回通信

c# - 如何从进程开始捕获所有应用程序/窗口消息?

c# - 总时间 (HH :MM) in RDLC

c# - 如何在输出到html之前停止asp.net编码字符?