首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将玩家存储在数组中?

如何将玩家存储在数组中?
EN

Stack Overflow用户
提问于 2012-11-29 20:04:39
回答 3查看 797关注 0票数 1

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

这就是我到目前为止所写的

代码语言:javascript
复制
    {
        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();
    }
}

}

EN

回答 3

Stack Overflow用户

发布于 2012-11-29 20:09:18

您是对的,只需分配大小为NumberofPlayers的数组即可

代码语言:javascript
复制
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];
}
票数 3
EN

Stack Overflow用户

发布于 2012-11-29 20:13:59

将数组的大小设置为NumberofPlayers。

代码语言:javascript
复制
PlayerPositions = new int [NumberofPlayers];
票数 1
EN

Stack Overflow用户

发布于 2012-11-29 20:09:41

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

代码语言:javascript
复制
List<PlayerPositions> players = new List<PlayerPositions>();

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

代码语言:javascript
复制
while (NumberOfPlayers > 0)
{
players.Add(new PlayerPositions());
NumberOfPlayers--;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13625451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档