如何将玩家存放在阵列中?

时间:2022-01-04 16:49:38

Pretty basic but I am creating a game and I will have 2 to 4 players in the game I need to know how I can ask the user how many players there will be then store this amount in my array for later us?!

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

this is what I wrote so far

这是我到目前为止写的

    {
        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();
    }
}

}

3 个解决方案

#1


3  

You are on the right track, just allocate the array with size of NumberofPlayers

您处于正确的轨道上,只需分配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];
}

#2


1  

Set size of array as NumberofPlayers.

将数组大小设置为NumberofPlayers。

PlayerPositions = new int [NumberofPlayers];

#3


0  

For this, why don't you just create a list of PlayerPositions.

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

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

Depending on the input from the user, add those many objects to the above list.

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

while (NumberOfPlayers > 0)
{
players.Add(new PlayerPositions());
NumberOfPlayers--;
}

#1


3  

You are on the right track, just allocate the array with size of NumberofPlayers

您处于正确的轨道上,只需分配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];
}

#2


1  

Set size of array as NumberofPlayers.

将数组大小设置为NumberofPlayers。

PlayerPositions = new int [NumberofPlayers];

#3


0  

For this, why don't you just create a list of PlayerPositions.

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

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

Depending on the input from the user, add those many objects to the above list.

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

while (NumberOfPlayers > 0)
{
players.Add(new PlayerPositions());
NumberOfPlayers--;
}