码迷,mamicode.com
首页 > 其他好文 > 详细

第八天学习内容 集合

时间:2015-03-09 09:18:08      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace day08
{
    class Program
    {
        static void Main(string[] args)
        {
//彩票,双色球选号
              while (true)//一直循环
               {
                   Random r = new Random();//创建随机函数r
                   int[] caipiao = new int[7];//创建数组caipiao含7个元素
                   for (int i = 0; i < 6; )//for循环给红球赋值
                   {
                       if (i == 0)//第一个红球 赋值
                       {
                           caipiao[i] = r.Next(1, 34);//随机函数取值1-33之间为caipiao[0]元素赋值
                           i++;//I++
                       }
                       else
                       {
                           int temp = r.Next(1, 34);//创建一个临时变量,值为随机1-33之间


                           for (int j = 0; j < i; )//循环遍历caipiao数组0-I位的元素
                           {
                               if (temp == caipiao[j])//比较临时变量temp与caipiao[j]的值是否相等
                               {
                                   temp = r.Next(1, 34);//如果相等重新为temp赋值,
                                   j = 0;                 //并且将j的值修改为0,用temp从头循环比较
                               }
                               else
                               {
                                   j++;        //如果无重复 则J++
                               }
                           }

                           caipiao[i] = temp;      //结束循环比较时将temp赋值给caipiao[i]元素
                           i++;                    //I++

                       }
                   }
                   for (int i = 0; i < caipiao.Length - 1; i++)   //冒泡排序!
                   {
                       for (int j = i + 1; j < caipiao.Length - 1; j++)
                       {
                           int temp1 = 0;
                           if (caipiao[i] > caipiao[j])
                           {

                               temp1 = caipiao[i];
                               caipiao[i] = caipiao[j];
                               caipiao[j] = temp1;

                           }
                       }
                   }
                   caipiao[6] = r.Next(1, 17);//蓝球赋值
                   for (int i = 0; i < caipiao.Length; i++)//循环输出所有值
                   {
                       if (i == 6)
                       {
                           Console.Write("蓝球:" + caipiao[i] + "\t");
                       }
                       else
                       {
                           Console.Write(caipiao[i] + "\t");
                       }

                   }
            
                   Console.ReadLine();

               }   

  

  //   arraylist集合
            ArrayList al = new ArrayList();
            al.Add(3);    //向集合中放入第一个数据,索引是0
            al.Add("你");//依次向集合中放入数据
            al.Add(5);
            al.Add(7);
            al.Insert(1, 9);//在索引1的位置插入9这个数,后面的数据的索引依次靠后
            al.Remove(7);  //把从索引0开始的第一个数据为7的删除
            al.RemoveAt(1);//把索引为1的数据删除
            int x = al.IndexOf("你"); //查找集合中“你”的索引
            ArrayList xal = new ArrayList();
            xal = (ArrayList)al.Clone();  //新建一个集合并将al集合中的元素复制到其中
            for (int i = 0; i < xal.Count; i++)
            {
                Console.WriteLine(xal[i]);
            }
            bool c = xal.Contains(3);  //判断xal集合中是否含有3这个元素
            Console.WriteLine(c);
            Console.WriteLine("‘你’的索引是" + x);
            Console.ReadKey(); 

 

//输入学生成绩并计算平均分
            ArrayList cj=new ArrayList();
            Console.Write("请输入人数:");
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                Console.Write("请输入第"+(i+1)+"位同学的成绩:");
                cj.Add(Convert.ToInt32(Console.ReadLine()));
            }
            double  sum = 0;
            for (int i = 0; i < n; i++)
            {
                sum +=Convert.ToDouble(cj[i]);
               
            }
            cj.Sort();  //排序指令,升序
          //  cj.Reverse();  // 翻转指令,执行之后变为降序排列
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine(cj[i]);
            }

            double avr = sum / n;
            Console.WriteLine("平均分为"+avr);
            Console.ReadKey();


// stack集合
            Stack ss = new Stack();
            ss.Push("1");
            ss.Push("2");
            ss.Push("3");
            ss.Push("4");//为stack集合赋值
            string tc = ss.Pop().ToString();//定义一个字符串接收弹出的元素
             * //string tcc =ss.Peek().ToString();//只获取最后一个进入的数据,不移除
            foreach (string item in ss)
            {
                Console.WriteLine(item);//输出元素弹出后的集合
            }
            Console.WriteLine("有"+ss.Count+"个元素");
            Console.WriteLine("弹出元素为"+tc);
            Console.ReadKey();  

 


            Queue que = new Queue();
            que.Enqueue(1);
            que.Enqueue(2);
            que.Enqueue(3);
            que.Enqueue(4);
            int chu = Convert.ToInt32(que.Dequeue().ToString());
            foreach (int item in que)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("有" + que.Count + "个元素");
            Console.ReadKey();  */


 

第八天学习内容 集合

标签:

原文地址:http://www.cnblogs.com/William-1234/p/4322687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!