1

非常基本,但我正在创建一个游戏,我将在游戏中有 2 到 4 个玩家我需要知道如何询问用户会有多少玩家,然后将这个数量存储在我的数组中以供以后使用?!

这是我到目前为止写的

    {
        int NumberofPlayers;
        {
            do
            {
                Console.WriteLine("Please enter number of players (2-4): ");
                String StringNumberOfPlayers = Console.ReadLine();
                NumberofPlayers = int.Parse(StringNumberOfPlayers);

            }
            while (NumberofPlayers > 4 || NumberofPlayers < 2);
        }


        // need get the number of players and set the required elements in
        // playerPositions to 0 on the board
    }
            static int [] PlayerPositions = new int [4];

    static void Main()
    {
        ResetGame();
    }
}

}

4

3 回答 3

3

你在正确的轨道上,只需分配大小为的数组NumberofPlayers

static int [] PlayerPositions;

public void ResetGame()
{
    int NumberofPlayers;
    do
    {
        Console.WriteLine("Please enter number of players (2-4): ");
        String StringNumberOfPlayers = Console.ReadLine();
        NumberofPlayers = int.Parse(StringNumberOfPlayers);
    }
    while (NumberofPlayers > 4 || NumberofPlayers < 2);

    // need get the number of players and set the required elements in
    // playerPositions to 0 on the board
    PlayerPositions = new int [NumberofPlayers];
}
于 2012-11-29T12:09:17.610 回答
1

将数组的大小设置为 NumberofPlayers。

PlayerPositions = new int [NumberofPlayers];
于 2012-11-29T12:13:58.600 回答
0

为此,您为什么不创建一个 PlayerPositions 列表。

List<PlayerPositions> players = new List<PlayerPositions>();

根据用户的输入,将这些对象添加到上面的列表中。

while (NumberOfPlayers > 0)
{
players.Add(new PlayerPositions());
NumberOfPlayers--;
}
于 2012-11-29T12:09:41.370 回答