格式异常未处理

时间:2014-11-18 04:15:25

标签: c#

我创建了一个玩家类,我正在尝试使用我的玩家类为我的菜单驱动的玩家程序更新玩家。我希望能够让用户输入一个玩家编号并询问他们是否想要更新玩家助攻和目标然后显示更新的玩家信息,如果玩家不存在那么它会显示消息玩家没有存在。当我去更新播放器时,我不断收到错误:未处理的类型' System.FormatException'发生在mscorlib.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();

1 个答案:

答案 0 :(得分:0)

我认为你想要{2}代替{3}

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

错误的含义只是您要求索引3处的值,而您没有传入。