C# 控制台 数字洗牌程序-整数数组自定、洗牌次数自定

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections;//ArrayList必备
using System.Threading;//时间延迟必备

namespace ConsoleApplication2
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
int[] b = new int[] { 0,1,2,3,4,5,6,7,8,9};
            Console.WriteLine(
"数字牌原样输出如下:");
            Print(b);
            Console.Write(
"请问要洗多少次牌?");
            
int Num = int.Parse(Console.ReadLine());

            Console.WriteLine();
            
for (int i = 0; i < Num; i++)
            {
                Shuffle(
ref b);
                Console.WriteLine(
"正在洗牌,请稍后");
                Thread.Sleep(
1000);
                Print(b);
            }
            Console.ReadKey();

        }
        
static int RandomIndex(int[] a)
        {
//产生数组下标中的随机数
            Random r=new Random();
            
int i = r.Next(0,a.Length);
            
return i;
        }

        
static void DelectIndex(ref int[] a, int i)
        {
//删除数组中第i个元素
            ArrayList arr1 = new ArrayList();
            
for (int k = 0; k < a.Length; k++)
                
if (k != i)
                    arr1.Add(a[k]);
            
int[] b = (int[])arr1.ToArray(typeof(int));
            a 
= b;
        }

        
static void Shuffle(ref int[] a)
        { 
//洗牌函数,将数组彻底打乱
            ArrayList arr1 = new ArrayList();
            
int i=0;//用于产生随机下标
            while (a.Length != 0)
            {
                 i
= RandomIndex(a);
                arr1.Add(a[i]);
                DelectIndex(
ref a, i);
            }
            
int[] b = (int[])arr1.ToArray(typeof(int));
            a 
= b;
        }

        
static void Print(int[] a)
        {
//打印数组函数
            foreach (int p in a)
                Console.Write(
"{0,-4}",p);
            Console.WriteLine();
        }
    }
}

转载于:https://www.cnblogs.com/zhangzufa/archive/2009/04/30/1446625.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值