更新方法不更新

时间:2014-11-17 20:50:26

标签: c#

我创建了一个玩家类,我正在尝试使用我的玩家类为我的菜单驱动的玩家程序更新玩家。我希望能够让用户输入一个玩家编号并询问他们是否想要更新玩家助攻和目标然后显示更新的玩家信息,如果玩家不存在那么它会显示消息玩家没有存在。出于某种原因,当我输入我创建的其中一个玩家的玩家号码时,它仍然表示玩家即使刚刚创建也不存在。我不知道如何解决这个问题。

任何正确方向的帮助或指导都将不胜感激,谢谢

static void ProcessUpdate(Int32 number, String firstName, String lastName, Int32 goals,
        Int32 assists, Player[] players, ref Int32 playerCount, Int32 MAXPLAYERS)
    {
        int player;// Player number to find
        int playerindex;//index of the player number in Array
        string answer, answer2;
        //String lastName;
        //int points;

        if (playerCount < MAXPLAYERS )
        {
            Console.WriteLine("\nUpdate Player: please enter the player's number");
            player = 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, players[playerindex].Points());
                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");
        }
    }
  

如果需要,这是GetPlayerIndex

//Returns the index of the player number in the table 
    //or -1 if the number is not found
    static Int32 GetPlayerIndex(Int32 number, String firstName, String lastName, Int32 goals,
        Int32 assists, Player[] players, ref Int32 playerCount)
    {
        Int32 index = 0;
        bool found = false;
        while (index < playerCount && found == false)
             if (players[index].Number == number)
                found = true;
              else
                index++;
        if (found == false)
            index = -1;
        return index;
    }

1 个答案:

答案 0 :(得分:0)

尝试更改此行:

playerindex = GetPlayerIndex(number, firstName, lastName, goals, assists, players, ref playerCount);

对此(只是为了让它工作):

playerindex = GetPlayerIndex(player, firstName, lastName, goals, assists, players, ref playerCount);

如果您有权访问Linq(我当然希望如此,除非这是针对某个课程的?),我建议您保留一个List<Player>而不是Player[],并使用类似于以下调用的内容按号码找到玩家:

var player = players.Where(x => x.Number == number).FirstOrDefault();

在此之后,如果player == null,那么您知道该播放器不存在。