格式异常未处理

本文关键字:未处理 异常 格式 | 更新日期: 2023-09-27 18:30:40

我创建了一个播放器类,我正在尝试使用我的播放器类为我的菜单驱动的播放器程序更新播放器。我希望能够让用户输入球员编号并询问他们是否要更新球员助攻和进球,然后显示更新的球员信息,如果球员不存在,则会显示消息球员不存在。当我去更新播放器时,我不断收到错误:mscorlib 中发生了类型为"System.FormatException"的未处理异常.dll其他信息:索引(从零开始)必须大于或等于零且小于参数列表的大小。我不确定如何解决这个问题。

任何帮助将不胜感激。

static void ProcessUpdate(Int32 number, String firstName, String lastName, Int32 goals,
    Int32 assists, Player[] players, ref Int32 playerCount, Int32 MAXPLAYERS)
{       
    int playerindex;//index of the player number in Array
    string answer, answer2;
    if (playerCount < MAXPLAYERS )
    {
        Console.WriteLine("'nUpdate Player: please enter the player's number");
        number = Int32.Parse(Console.ReadLine());
        playerindex = GetPlayerIndex(number, firstName, lastName, goals, assists, players, ref playerCount);
        if (playerindex != -1)
        {                
            Console.WriteLine("'nUpdate Player: Player {0} currently has {1} goals and {3} assists", players[playerindex].Number,
                    players[playerindex].Goals, players[playerindex].Assists);
            Console.ReadLine();
            Console.WriteLine("'nEdit Goals?: Y/N");
            answer = Console.ReadLine();
            if (answer.Equals("Y"))
            {
                Console.WriteLine("'nUpdate Player: please enter the player's new Goal total");
                goals = Int32.Parse(Console.ReadLine());
                Console.WriteLine("'nEdit Assists?: Y/N");
                answer2 = Console.ReadLine();
                if (answer2.Equals("Y"))
                {
                    Console.WriteLine("'nUpdate Player: please enter the player's new Assists total");
                    assists = Int32.Parse(Console.ReadLine());
                    players[playerindex].LastName = lastName;
                    players[playerindex].FirstName = firstName;
                    players[playerindex].Goals = goals;
                    players[playerindex].Assists = assists;
                    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 Updated!");
                    Console.WriteLine();
                }
            }
            else
                Console.WriteLine("'nUpdate Player: the player number does not exists");
        }
        else
            Console.WriteLine("'nUpdate Player: the player does not exist in the roster");
    }
}

这是发生错误的行

Console.WriteLine("'nUpdate Player: Player {0} currently has {1} goals and {3} assists", players[playerindex].Number,
                        players[playerindex].Goals, players[playerindex].Assists);
                Console.ReadLine();

格式异常未处理

我认为你想要{2}而不是{3}

"'nUpdate Player: Player {0} currently has {1} goals and {3} assists"

错误的含义只是您在索引 3 处请求一个值,而您没有传入该值。