c#数组内数据打乱

简化版本

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] str = new string[] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","大王","小王" };
            string[] str2 = Xipai(str);
            for (int i = 0; i < str2.Length; i++)
            {
                Console.WriteLine(str2[i]);
            }
            Console.ReadLine();
        }
        static string[] Xipai(string[] str1)
        {
            Random rnd = new Random();

            for (int i = 0; i < str1.Length; i++)
            {
                int c = rnd.Next(str1.Length);
                string temp = str1[i];
                str1[i] = str1 [c];
                str1[c]= temp;
            }
            return str1;
        }
    }
}

优化版本

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine(Join1(Card()));
            Console.ReadLine();

        }




        static string Join1(string[] arr, string link = ",")
        {
            string str = "";
            for (int i = 0; i < arr.Length; i++)
            {
                str += arr[i];
                if (i != str.Length - 1)
                {
                    str += link;
                }
            }
            return str;
        }
        static string[] Remove(string[] arr, int index)
        {
            //判断索引是否超出了范围
            if (index >= arr.Length)
            {
                return arr;
            }
            string[] newArr = new string[arr.Length - 1];
            //记录新数组元素存储的位置
            int count = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                if (i != index)
                {
                    newArr[count++] = arr[i];
                }

            }
            //返回删除元素后的新数组
            return newArr;
        }
        static string[] Add(string[] arr, string item)
        {
            string[] newArr = new string[arr.Length + 1];
            for (int i = 0; i < arr.Length; i++)
            {
                newArr[i] = arr[i];
            }
            newArr[newArr.Length - 1] = item;
            return newArr;
        }
        static string[] Card()
        {
            string[] cards = {
                "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A","大王","小王"


            };


            //洗牌之后的扑克牌列表
            string[] newCards = { };

            //随机数对象
            Random random = new Random();

            while (cards.Length > 0)
            {
                //随机取扑克牌的索引
                int index = random.Next(cards.Length);
                //将随机的扑克牌添加到列表中
                newCards = Add(newCards, cards[index]);

                //删除当前取出来的扑克牌
                cards = Remove(cards, index);
            }
            return newCards;
        }










    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值