C#数组_160921

来源:互联网 发布:淘宝寄错东西怎么办 编辑:程序博客网 时间:2024/06/04 18:25

double[] y = new double[3];声明部分跟C不同了。。。

y.length();可以直接查看数组长度

char[] b=new char[]{'a', 'b', 'c'};初始化

foreach循环使可以不依赖索引而读取每个数组元素,

=============一个栗子============================

            int[] num = { 3, 34, 42, 2, 11, 19, 30, 55, 20 };

            //int x;
            foreach (int x1 in num)
            {
                if (x1 % 2 == 0)
                    Console.WriteLine(x1);
            }

二维数组

int[ ,] arr=new int[2,3]//两个一维数组,每个数组包含3个变量,总共六个元素

访问arr[1,0]

======================一个栗子=========================

            int[,] score = new int[4, 2] { { 89, 86 }, { 69, 40 }, { 64, 92 }, { 82, 100 } };
            Console.WriteLine("the score are:");
            for (int i = 0; i < score.GetLongLength(0); i++)
            {
                Console.WriteLine("语文:{0},数学{1}",score[i,0],score[i,1]);          
            }

======================================================

int.Parse(Console.RaedLine());//把输入转换为int型

            string[] name = new string[5];
            int[] score = new int[5];
            for (int i = 0; i < name.Length; i++)
            {
    
                Console.Write("please input the name:  ");
                name[i] = Console.ReadLine();
                Console.Write("please input the score:  ");
                score[i] = int.Parse(Console.ReadLine());

            }

            int sum = 0, avg;
            for (int i = 0; i < name.Length; i++)
            {
                sum = sum + score[i];

            }
            avg = sum / 5;
            Console.Write(avg);



0 0
原创粉丝点击