二维数组,多维数组

二维数组,多维数组
//定义二维数组
 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             }
方法二:
 

 

 

转载于:https://www.cnblogs.com/maxin991025-/p/5964285.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值