前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >随机点名

随机点名

作者头像
祝你万事顺利
发布2019-05-29 16:02:10
1K0
发布2019-05-29 16:02:10
举报
文章被收录于专栏:Unity游戏开发Unity游戏开发

每次点名从1到number 被点过的数字不会出现,直到全部数字被点一遍 程序打开读取上次点名信息,关闭存储点名信息

代码语言:javascript
复制
class Program
    {
        static int number = 9;
        static List<int> arr = new List<int>();
        static void Main(string[] args)
        {
            rollCall();
        }
        static void rollCall()
        {
            string[] arrStrs = File.ReadAllLines("arrText.txt");
            if(arrStrs.Length > 0)
            {
                foreach (var item in arrStrs)
                {
                    arr.Add(int.Parse(item));
                }
            }
            
            while (Console.ReadLine() != "end")
            {
                Console.WriteLine(GetRandomNumber(arr));
            }

            string[] tempArr = new string[arr.Count];
            for (int i = 0; i < arr.Count; i++)
            {
                tempArr[i] = arr[i].ToString();
            }
            File.WriteAllLines("arrText.txt", tempArr);

        }
        static int GetRandomNumber(List<int> arr)
        {
            if(arr.Count <= 0)
            {
                int arrLength = number;
                for(int i = 0; i < arrLength; i++)
                {
                    arr.Add(i + 1);
                }
            }
            Random random = new Random();
            int randomIndex = random.Next(0, arr.Count);
            int temp = arr[randomIndex];
            arr.RemoveAt(randomIndex);
            return temp;
        }
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.05.15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档