C#练习 抓阄
using System; namespace ZhuaJiuNS { public class ZhuaJiu { public static void Main() { string[] namelist = null; CreateNameList(ref namelist); string[] afternamelist; Zhua(namelist, out afternamelist); PrintNameList(afternamelist); Console.ReadKey(); } public static void CreateNameList(ref string[] beforelist) { beforelist = new string[5]; beforelist[0] = "张三"; beforelist[1] = "李四"; beforelist[2] = "王五"; beforelist[3] = "赵六"; beforelist[4] = "孙七"; } public static void Zhua(string[] beforelist, out string[] afterlist) { afterlist = new string[1]; int tmpindex = -1; System.Random ran = new Random(); if (beforelist.Length > 0) { Console.WriteLine("开始抓阄了:"); int tmplength = beforelist.Length; for (int i = 0; i < tmplength; i++) { Console.WriteLine("请回车进行抓阄"); Console.ReadLine(); tmpindex = ran.Next(0, beforelist.Length); Console.WriteLine("本次抓到的人为" + beforelist[tmpindex]); InsertAfterList(beforelist[tmpindex], ref afterlist); RemoveFromBeforeList(tmpindex, ref beforelist); } Console.WriteLine("抓阄结束"); } } public static void InsertAfterList(string insertname, ref string[] afterlist) { afterlist[afterlist.Length - 1] = insertname; string[] longlist = new string[(afterlist.Length + 1)]; for (int i = 0; i < afterlist.Length; i++) { longlist[i] = afterlist[i]; } afterlist = longlist; longlist = null; } public static void RemoveFromBeforeList(int removeindex, ref string[] beforelist) { if (removeindex == (beforelist.Length - 1)) { beforelist[removeindex] = ""; } else { int i = 0; for (i = removeindex; i < (beforelist.Length - 1); i++) { beforelist[i] = beforelist[i + 1]; } } if (beforelist.Length > 1) { string[] shortlist = new string[(beforelist.Length - 1)]; for (int i = 0; i < beforelist.Length - 1; i++) { shortlist[i] = beforelist[i]; } beforelist = shortlist; shortlist = null; } else { beforelist = null; } } public static void PrintNameList(string[] list) { Console.WriteLine("下面输出人员列表:"); for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + "\t"); } Console.WriteLine(); Console.WriteLine("输出人员列表结束"); } } }