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