码迷,mamicode.com
首页 > 编程语言 > 详细

2014-12-15 数组

时间:2014-12-15 17:07:08      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:ar   sp   for   strong   on   2014   问题   bs   ad   

数组:定义一组同类型的指定个数的变量,索引从0开始

int [] shuzuming=new int[10];   //中括号中为需要定义的长度(个数)

shuzuming[0]=1;

shuzuming[1]=2;

 

//彩票双色球问题(红球33选6,蓝球16选1)

int[] hongqiu=new int[6];
            Random r=new Random();
            for(int i=0;i<6;i++)
            {
                hongqiu[i] = r.Next(1, 34);
                for (int j = 0; j < i; j++)
                {
                    if (hongqiu[j] == hongqiu[i])
                    {
                        i--;        //如果第j个红球等于第i个红球的话,跳出循环,让第j个红球与第i-1求红球比较
                        break;
                    }
                }
            }

 

//输入10个分数,求总分和平均分

int[] fenshu = new int[10];
            for (int i = 0; i <10; i++)
            {
                Console.WriteLine("请输入第{0}个分数", i+1);
                fenshu[i] = int.Parse(Console.ReadLine());
            }
            int sum = 0;
            for (int j = 0; j < 10; j++)
            {
                sum = sum + fenshu[j];
            }
            double avg=sum/fenshu.Length;
            Console.WriteLine("总分为{0}", sum);
            Console.WriteLine("平均分为{0}", avg);
            Console.ReadLine();

//求最高分最低分

int max = 0;
            int min = 0;
            for (int k = 0; k <= 10; k++)
            {
                if (max < fenshu[k])
                {
                    max = fenshu[k];
                }
                if (min > fenshu[k])
                {
                    min = fenshu[k];
                }
            }
            Console.WriteLine("最高分为{0}", max);
            Console.WriteLine("最低分为{0}", min);

 

foreach:

foreach(int a in shu):会自动读取数组中的每一个数,然后把值赋值给a

foreach(int a in fenshu)
            {
                Console.WriteLine(a);
            }

foreach其实相当于 for(int i=0;i<=shu.length;i++)

                               {

                                   int a;

                                   a=shu[i];

                                   console.writeline(a);

                                }

 

等量代换:需要中间变量

            int a = 3, b = 5;
            int c;
            c = a;
            a = b;
            b = c;

 

冒泡排序:

            for (int m = 0; m < 10 - 1; m++)
            {
                for (int n = m; n < 10; n++)
                {
                    if (fenshu[m] < fenshu[n])
                    {
                        int zhong;
                        zhong = fenshu[m];
                        fenshu[m] = fenshu[n];
                        fenshu[n] = zhong;
                    }
                }
            }

2014-12-15 数组

标签:ar   sp   for   strong   on   2014   问题   bs   ad   

原文地址:http://www.cnblogs.com/jintuo/p/4164593.html

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