欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

二维数组,多维数组

程序员文章站 2024-03-04 16:41:23
...

二维数组,多维数组
//定义二维数组
 int[,] a=new int [3,2]     //3表示有三个一维数组,2表示每一个一位数组中有两个元素

int[,] a=new int [3,4]{
            {1,2,3,4},
            {5,6,7,8},
            {9,8,7,6}
            };
            //打印出来
            for (int i = 0; i < 3;i++ )
            {
                for(int j=0;j<4;j++)
                {
                    Console.Write(a[i,j]+"\t");
                }
                Console.WriteLine();
            }
二维数组,多维数组

  

//输入班级人数,输入每人的语数英成绩再打印出来,求语文两个最高分,数学两个最低分,英语平均分 

二维数组,多维数组二维数组,多维数组
 1 Console.Write("输入班级人数:");
 2             int n = int.Parse(Console.ReadLine());
 3             double[,] a = new double[n, 3];
 4             for (int i = 0; i < n; i++)
 5             {
 6                 Console.Write("输入第{0}个人的语文成绩:", i + 1);
 7                 a[i, 0] = double.Parse(Console.ReadLine());
 8                 Console.Write("输入第{0}个人的数学成绩:", i + 1);
 9                 a[i, 1] = double.Parse(Console.ReadLine());
10                 Console.Write("输入第{0}个人的英语成绩:", i + 1);
11                 a[i, 2] = double.Parse(Console.ReadLine());
12             }
13             Console.WriteLine("输入完毕!");
14             for (int i = 0; i < n; i++)
15             {
16                 for (int j = 0; j < 3; j++)
17                 {
18                     Console.Write(a[i, j] + "\t");
19                 }
20                 Console.WriteLine();
21             }
22             double sum = 0;
23             for (int i = 0; i < n - 1; i++)
24             {
25                 for (int j = i + 1; j < n; j++)
26                 {
27                     if (a[i, 0] < a[j, 0])
28                     {
29                         double t = a[i, 0];
30                         a[i, 0] = a[j, 0];
31                         a[j, 0] = t;
32                     }
33                     if (a[i, 1] > a[j, 1])
34                     {
35                         double t = a[i, 1];
36                         a[i, 1] = a[j, 1];
37                         a[j, 1] = t;
38                     }
39                 }
40                 sum += a[i, 2];
41             }
42             Console.WriteLine("语文两个最高分为{0},{1};数学两个最低分为{2},{3};英语平均分为{4}", a[0, 0], a[0, 1], a[1, 0], a[1, 1], sum / n);
View Code

二维数组,多维数组

 

//打印“马”这个字

二维数组,多维数组二维数组,多维数组
 1 string[,] a = new string[,]{
 2             {"  ","","","","","  ",""},
 3             {"  ","  ","","  ","","",""},
 4             {"  ","  ","","  ","","",""},
 5             {"  ","  ","","","","",""},
 6             {"  ","  ","  ","  ","  ","  ",""},
 7             {"","","","","","",""},
 8             {"  ","  ","  ","  ","  ","  ",""},
 9             {"  ","  ","  ","  ","  ","",""},
10         };
11             for (int i = 0; i < 8; i++)
12             {
13                 for (int j = 0; j < 7; j++)
14                 {
15                     Console.Write(a[i, j]);
16                 }
17                 Console.WriteLine();
18             }
View Code

二维数组,多维数组

 

//一维数组是一条线,二维数组是一个面(表格)
//三维数组是一个立体(教学楼)
//int[,,]a=new int[,,];

//split() 以***进行分割
//分割出来的内容放置在string类型中,不需要给数组定义长度

            Console.Write("请输入姓名,年龄,工作单位:");
            string s = Console.ReadLine();
            string[] a = s.Split(',');
            foreach (string aa in a)
            {
                Console.WriteLine(aa);
            }
二维数组,多维数组

  

 //春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知道多少。替换文字

 

二维数组,多维数组二维数组,多维数组
1 string a = "春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知道多少。";
2             a = a.Replace("", "这儿");
3             Console.WriteLine(a);
方法一:
二维数组,多维数组

 

二维数组,多维数组二维数组,多维数组
 1 string[,] a = new string[4, 6]
 2             {
 3                 {"","","","","",""},
 4                 {"","","","","",""},
 5                 {"","","","","",""},
 6                 {"","","","","",""}
 7             };
 8             for (int i = 0; i < 4; i++)
 9             {
10                 for (int j = 0; j < 6; j++)
11                 {
12                     if (a[i, j] == "")
13                     {
14                         a[i, j] = "这儿";
15                     }
16                     Console.Write(a[i, j]);
17                 }
18                 Console.WriteLine();
19             }
方法二:
 二维数组,多维数组

 

 

上一篇: [LeetCode] 344. 反转字符串

下一篇: