C#練習 抓鬮

來源:互聯網
上載者:User

標籤:style   blog   color   for   div   re   

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("輸出人員列表結束");        }    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.