C#将一个数组分成多个较小的数组(随机)

时间:2017-08-14 13:26:39

标签: c# .net arrays discord

我正在为我的朋友和我在c#中制作一个DISCORD.NET BOT,这是我第一次使用这种语言(我使用的是VB.NET,但语法对我来说是全新的)。

我的问题"是我的一个命令"!小队"接收任意数量的用户名,并将用户分成小组,这样我们就不必自己做,并且对最后被选中的人感到不满。我不会解释下面包含的代码并且非常简单。

我的问题围绕着输入6个或7个名字,5个很简单。但是我希望有6支球队分别是4支球队和2支球队,以及7支两支球队,分别是4支球队和3支球队。

以下是我对如何做到这一点的想法,但我认为它会很混乱,所以我想要一些指示!对于6个名称,创建2个阵列,最大项目为2和4,首先随机填充2,然后将其余部分推到4.使用7执行相同操作,首先填充3个团队,然后将其余部分推送到4。我相信这比从数组到数组的来回更有效。但是,我需要从主阵列中删除所选用户,每次重新索引数组,这样我就不会再次选择同一个人,并且还会更改随机生成器中的最大数量。选择索引。我可以做到这一切,但它会如此草率,有没有办法干净地做60-70行for和if循环?

这是我的代码,谢谢! :)

[Command("Squads")]
    public async Task CreateSquads(params string[] usersArray)
    {

        int totalUsers = usersArray.Length;
        Random squadSelector = new Random();
        string squadsMessage;

        if (totalUsers < 5)
        {
            squadsMessage = "I am sensing 4 or less names, just play squads, clowns.";
        }
        else if (totalUsers == 5)
        {
            string selectedUser = usersArray[squadSelector.Next(0, usersArray.Length)];
            squadsMessage = "Tough luck " + selectedUser + ", better luck next time!";
        }
        else if (totalUsers == 6)
        {
            // Split users into two teams of 2 and 4
            squadsMessage = "6 people."; // Placeholder
        }
        else if (totalUsers == 7)
        {
            // Split users into two teams of 3 and 4
            squadsMessage = "7 people."; // Placeholder
        }
        else
        {
            // More than 7 people. (not coding for this)
            squadsMessage = "We have this many friends? Wut.";
        }

        // Originally used this just to spit out the names for testing
        //for (int i = 0; i < totalUsers.Length; i++)
        //{
        //    await Context.Channel.SendMessageAsync(totalUsers[i]);
        //}

        await Context.Channel.SendMessageAsync(squadsMessage);

更新的代码,完美的工作!谢谢大家! :)

[Command("Squads")]
    public async Task CreateSquads(params string[] usersArray)
    {

        string[] squadOne;
        string[] squadTwo;

        int totalUsers = usersArray.Length;
        Random squadSelector = new Random();
        StringBuilder sb = new StringBuilder();
        string squadsMessage;

        if (totalUsers < 5)
        {
            squadsMessage = "I am sensing 4 or less names, just play squads, morons.";
        }
        else if (totalUsers >= 5 && totalUsers <= 7)
        {
            // 5 users becomes 4 and 1; 6 users becomes 4 and 2; 7 users becomes 4 and 3
            usersArray = usersArray.OrderBy(i => squadSelector.Next()).ToArray();

            squadOne = usersArray.Take(4).ToArray();
            squadTwo = usersArray.Skip(4).Take(totalUsers - 4).ToArray();

            sb.Append("__**Squad One:**__");
            sb.Append(Environment.NewLine);

            for (int i = 0; i < squadOne.Length; i++)
            {
                sb.Append(squadOne[i]);
                sb.Append(Environment.NewLine);
            }

            sb.Append(Environment.NewLine);

            if (totalUsers == 5)
            {
                sb.Append("__**LOL, Loser:**__");
            }
            else
            {
                sb.Append("__**Squad Two:**__");
            }

            sb.Append(Environment.NewLine);

            for (int i = 0; i < squadTwo.Length; i++)
            {
                sb.Append(squadTwo[i]);
                sb.Append(Environment.NewLine);
            }

            squadsMessage = sb.ToString();
        }
        else
        {
            // More than 7 people.
            squadsMessage = "We have this many friends? Wut.";
        }

        await Context.Channel.SendMessageAsync(squadsMessage);

    }

1 个答案:

答案 0 :(得分:4)

我会这样做:

var usersArray = new string[] { "1", "2", "3", "4", "5", "6", "7" };
var r = new Random();
usersArray = usersArray.OrderBy(i => r.Next()).ToArray();
var firstGroup = usersArray.Take(3).ToArray();
var secondGroup = usersArray.Skip(3).Take(4).ToArray();

OrderBy(i =&gt; r.Next())的行将随机排序。然后,您可以轻松地分为两组中的项目。 您还可以将此代码提取到方法:)