3月12日 数组

时间:2022-11-05 14:05:47

数组:

一维数组:

第一种定义方式:

int[] shuzu = new int[5];
            //shuzu[0] = 1;
            //shuzu[1] = 2;
            //shuzu[2] = 3;
            //shuzu[3] = 4;
            //shuzu[4] = 5;
            //Console.WriteLine(shuzu[4]);

第二种定义方式:

int[] shuzu = new int[]{1,2,3,4,5};

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

 

二维数组:

//二维数组
            //int[,] shuzu = new int[3, 4];//3个数组,每个数组有4个
            //int[,] shuzu = new int[,] { };

//输入班级人数,将每个人的
            //语文、数学、英语成绩输入二维数组;
            Console.Write("输入班级人数");
            int a = int.Parse(Console.ReadLine());
            double[,] fen = new double[a, 3];
            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (j == 0)
                    {
                        Console.WriteLine("" + (i + 1) + "个人的语文成绩是:");
                    }
                    if (j == 1)
                    {
                        Console.WriteLine("" + (i + 1) + "个人的数学成绩是:");
                    }
                    if (j == 2)
                    {
                        Console.WriteLine("" + (i + 1) + "个人的英语成绩是:");
                    }
                    fen[i, j] = double.Parse(Console.ReadLine());
                }
            }
            Console.ReadLine();
 string [,] shuzu = new string[7,7] 
            { 

            {"  ","  ","  ",""," "," "," "},
            {"  ","  ",""," ",""," "," "},
            {"  ",""," ",""," ",""," "},
            {"  ","  ","  ",""," "," "," "},
            {"  ","  ","","",""," "," "},
            {"  ","  ","",""," "," "," "},
            {"  ","","","","",""," "},
            

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

 

 

三维数组:

//三维数组
            //int[, ,] shuzu = new int[3, 2, 4];//3个二维数组,每个二维数组有2个一维数组,每个一维数组有4个数据