数组(彩票)

//数组,一组同类型的数据,数组是有长度的,数组有索引的,索引从0开始

            //生成随机不重复的6位数彩票
            int[] shuzu = new int[7];//定义了一个长度为6的int类型数组
            Random r = new Random ();
            for (int i = 0; i < 6; i++) //循环生成6个数
            {
                shuzu[i] = r.Next(1, 34); //生成一个数
                bool isok = false;
                for (int j = 0; j < i; j++) //比较是否跟之前的数相等
                {
                    if (shuzu[j] == shuzu[i])
                    {
                        isok = true;
                    }
                }

                if (isok)

                {
                    i--;//如果跟之前的数相等,后退一步
                    continue;
                }
                
            }

            shuzu[6] = r.Next(1, 17);


            //输入你买的号码
            Console.Write("请输入红球6个,蓝球一个,逗号隔开:");
            string shuru = Console.ReadLine();
            string[] ren = shuru.Split(',');
            //判断中了几个红球
            int count = 0;
            for (int i = 0; i < 6; i++)

            {
                for (int j = 0; j < 6; j++)
                {
                    if (int.Parse(ren[i])==shuzu[j])
                    {
                        count++;
                    }
                }
            }
            //判断蓝球中没中
            bool islan = false;
            if(int.Parse(ren[6])==shuzu[6])
            {
                islan = true;
            }
            //输出电脑随机的中奖号码
            foreach (int m in shuzu)
            {
                Console.Write(m+"|");
            }
            //判断中几等奖
            if (count == 6 && islan)
            {
                Console.WriteLine("一等奖");
            }
            else if (count == 5 && !islan)
            {
                Console.WriteLine("二等奖");
            }
            else if (count == 4 && islan)
            {
                Console.WriteLine("三等奖");
            }
            else if (count == 5 && islan)
            {
                Console.WriteLine("四等奖");
            }

            else {
                Console.WriteLine("运气太差了,不要再买了");
            }
            Console.ReadLine();

  

posted @ 2015-11-23 10:11  小菜鸟。  阅读(299)  评论(0编辑  收藏  举报