3.12课·········数组

Posted 魅影星魂

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3.12课·········数组相关的知识,希望对你有一定的参考价值。

数组:具有相同类型的若干变量按有序的形式组织起来的一种形式。这些按序排列的同类数据元素的集合称为数组。
定义数组
int[] 变量名 = new int [n];

string[] myStringArray = new string[6];

int[] myArray = new int[] {1, 3, 5, 7, 9};

数组索引号从0开始
一维数组[相同数据类型]
第一种定义方式
int[] shuzu=new int[5];
for (int i = 0; i <= 5;i++ )//用for循环为shuzu赋值
{
shuzu[i] = i + 1;
}
索引从0开始
赋值
shuzu[0] = 1;
shuzu[1] = 2;
shuzu[2] = 3;
shuzu[3] = 4;
shuzu[4] = 5;

第二种定义方式
int[] shuzu=new int[]{1,2,3,4,5};

Console.WriteLine(shuzu[2]);//3

输入10个人的年龄,并求和

int[] age = new int[10];
            int sum = 0;
            for (int i = 0; i < 10; i++)
            {
                Console.Write("请输入第" + (i + 1) + "个人的年龄:");
                age[i]=int.Parse(Console.ReadLine());//获取这个数并转换成数值类型放进数组age
                sum += age[i];
            }
            Console.WriteLine("这10个人的年龄之和是" + sum);
            Console.ReadLine();

 输入人名放进数组,输出第五个人的名字

    string[] s = new string[10];
            for (int i = 0; i < 10;i++ )
            {
                Console.Write("请输入第"+(i+1)+"个人的名字:");
                s[i] = Console.ReadLine();
            }
            Console.Write("第五个人的名字是"+s[4]);
            Console.ReadLine();

 输入班级人数,根据人数挨个输入成绩,求平均分

    Console.Write("请输入班级人数:");
            int a = int.Parse(Console.ReadLine());
            dou
            double[] chengji = new double[a];
            double sum = 0;
            for (int i = 0; i < a; i++)
            {
                Console.Write("请输入第" + (i + 1) + "个人的成绩:");
                chengji[i] = double.Parse(Console.ReadLine());
                sum += chengji[i];
            }
            Console.WriteLine("" + a + "个人的平均分是" + sum / a);

 

二维数组
int[,] shuzu = new int[3, 4];
int[,] shuzu = new int[,] {
{1,2,3,4},
{5,6,7,8},
{6,7,8,9}
};

输入班级人数,将每个人的
语文,数学,英语成绩输入二维数组

            Console.Write("请输入班级人数:");
            int a = int.Parse(Console.ReadLine());
            double [,] shuzu = new double [a,3];

            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (j == 0)
                    {
                        Console.Write("请输入第"+(i+1)+"个人的语文成绩:");
                    }
                    if (j == 1)
                    {
                        Console.Write("请输入第" + (i + 1) + "个人的数学成绩:");
                    }
                    if (j == 2)
                    {
                        Console.Write("请输入第" + (i + 1) + "个人的英语成绩:");
                    }
                    shuzu[i, j] = double.Parse(Console.ReadLine());
                }
            }



                Console.ReadLine();

打印字:王,企:

string[,] shuzu = new string[,] {
{" ","■","■","■","■","■",""},
{" "," "," ","■","","",""},
{" "," "," ","■","","",""},
{" "," ","■","■","■","",""},
{" "," "," ","■"," "," ",""},
{" "," "," ","■"," "," ",""},
{"■","■","■","■","■","■","■"}
};

            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    Console.Write(shuzu[i, j]);
                }
                Console.WriteLine();
            }

 

技术分享

string[,] s = new string[,]{
{" "," "," ","●"," "," "," "},
{" "," ","●"," ","●"," "," "},
{" ","●"," ","●"," ","●"," "},
{"●"," "," ","●"," "," ","●"},
{" ","●"," ","●","●","●"," "},
{" ","●"," ","●"," " ," "," "},
{"●","●","●","●","●","●","●"}
};

            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {

                    Console.Write(s[i, j]);

                }
                Console.WriteLine();
            }
            Console.ReadLine();

技术分享

 

以上是关于3.12课·········数组的主要内容,如果未能解决你的问题,请参考以下文章

3.16课·········小结

3.13课·········函数

3.15课·········out传值(传址)

3.30课·········Marquee标签

3.26课·········window.document对象

3.22课·········CSS样式表