首页 > 代码库 > 2014-12-16 多维数组

2014-12-16 多维数组

多维数组:

int [,] shuzuming=new int[5,3]   //有5个长度为3的一维数组

二维数组读取时,逗号前的为y轴,逗号后面的为x轴(一维数组)

int[,] second = new int[2, 3] { { 3, 2, 5 }, { 6, 7, 8 } };

second[0,1]=9  即为在(1,0)坐标处的值替换为9

 

 //年龄,身高,姓名,人数,求平均年龄、身高排序

Console.WriteLine("请输入人数");
            int n=int.Parse(Console.ReadLine());
            string[,] second = new string[n, 3];
            decimal sum=0;
            for(int i=0;i<n;i++)
            {
                Console.WriteLine("姓名");
                second[i,0]=Console.ReadLine();
                Console.WriteLine("年龄");
                second[i,1]=Console.ReadLine();
                sum=sum+decimal.Parse(Console.ReadLine());
                Console.WriteLine("身高");
                second[i,2]=Console.ReadLine();
            }
            decimal avg = sum / n;
            Console.WriteLine("平均年龄为" + avg);
            for (int i = 0; i < n - 1; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (decimal.Parse(second[i, 2]) < decimal.Parse(second[j, 2]))
                    {
                        string zhong;
                        zhong = second[i, 0];
                        second[i, 0] = second[j, 0];
                        second[j, 0] = zhong;

                        string zhong1;
                        zhong1 = second[i, 1];
                        second[i, 1] = second[j, 1];
                        second[j, 1] = zhong1;

                        string zhong2;
                        zhong2 = second[i, 2];
                        second[i, 2] = second[j, 2];
                        second[j, 2] = zhong2;
                    }
                }
            }


            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write(second[i, j] + "     ");
                }
                Console.Write("\n");
            }

2014-12-16 多维数组