C#二维数组

数组格式

 1             一维数组:
 2             Console.WriteLine("输入班级人数");
 3             int renshu = int.Parse(Console.ReadLine());
 4             int[] chengji = new int[renshu];   。。。。。。。。。。 初始化     1
 5             for (int i = 0; i < renshu ;i++ )
 6             {
 7                 Console.WriteLine("输入第{0}个人的成绩;",i+1);
 8                 chengji[i] = int.Parse(Console.ReadLine());。。。。。。。赋值
 9             }
10             Console.WriteLine("打印成绩");
11             Console.ReadLine();
12             Console.Clear();
13             for (int i = 0; i < renshu;i++ )
14             {
15                 Console.Write(chengji[i]+"\t");。。。。。。。。。。。。。打印方法一
16             }
17 
18 
19             //foreach(int aa in chengji  )
20             //{
21             //    Console.WriteLine(aa);   。。。。。。。。。打印方法2
22             //}
23             Console.ReadLine();
数组格式1举例

 

二维数组

 

 1 int[,] array = new int[3, 4]{           
 2 
 3 {1,2,3,4},            
 4 
 5  {3,4,5,6},             
 6 
 7 {5,6,7,8} };            
 8 
 9 
10 
11 3表示,有三个一维数组            
12 
13 4表示,每一个一维数组中有4个元素
二维数组

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

1             Console.Write("请输入姓名-年龄-工作单位:");
2             //"张三-33-汉企"
3             string s = Console.ReadLine();
4             string[] array = s.Split('-');
5             foreach (string aa in array)
6             {
7                 Console.WriteLine(aa);
8             }
split例题

打印图形

 1           
 2             string[,] wang = new string[,] {
 3             {"  ","","","","","",""},
 4             {"  ","  ","  ","","","",""},
 5             {"  ","  ","  ","","","",""},
 6             {"  ","  ","","","","",""},
 7             {"  ","  ","  ","","","",""},
 8             {"  ","  ","  ","","","",""},
 9             {"","","","","","",""}
10             };
11             for (int i = 0; i < 7;i++ )
12             {
13                 for (int j = 0; j < 7;j++ )
14                 {
15                     Console.Write(wang[i,j]);
16                 }
17                 Console.WriteLine();
18             }
打印一个王字

一维数组例题

 1             输入班级人数,输入每个人的人名及分数
 2             排序,知道最高分是多少,是谁考得
 3             Console.Write("请输入班级人数:");
 4             int a = int.Parse(Console.ReadLine());
 5             string [] name = new string[a];
 6             double [] score =new double[a];
 7 
 8             for (int i = 0; i < a;i++ )
 9             {
10                 Console.Write("请输入第{0}个姓名:",(i+1));
11                 name[i] = Console.ReadLine();
12                 Console.Write("请输入第{0}个人的分数:",(i+1));
13                 score[i] = double.Parse(Console.ReadLine());
14             }
15 
16             for (int i = 0; i < a - 1;i++ )
17             {
18                 for (int j = i + 1; j < a;j++ )
19                 {
20                     if (score[i] < score[j])
21                     {
22                         double zhong = score[i];
23                         score[i] = score[j];
24                         score[j] = zhong;
25                         string zh = name[i];
26                         name[i] = name[j];
27                         name[j] = zh;
28                     }
29                 }
30             }
31             Console.WriteLine("最高分的学生是:{0},他的分数是:{1}",name[0],score[0]);
打印最高分方法一

 

//输入班级人数,输入每个人的人名及分数
            //排序,知道最高分是多少,是谁考得
            //要求使用一个一维数组
            Console.Write("请输入班级人数:");
            int renshu = int.Parse(Console.ReadLine());
            string[] str = new string[renshu*2];
            int zhi = 0;
            for (int i = 0; i < renshu * 2; i += 2)
            {    
                zhi++;
                Console.Write("请输入第{0}个人的姓名:",zhi);
                str[i] = Console.ReadLine();
                Console.Write("请输入第{0}个人的成绩:", zhi);
                str[i+1] = Console.ReadLine();
                
            }
            for (int i = 0; i < renshu * 2-2; i += 2)
            {
                for (int j = i + 3; j < renshu * 2; j += 2)
                {
                    if (double.Parse(str[i + 1]) < double.Parse(str[j]))
                    {
                        string zhong = str[i];
                        str[i] = str[j - 1];
                        str[j - 1] = zhong;
                        zhong = str[i + 1];
                        str[i + 1] = str[j];
                        str[j] = zhong;
                    }
                }
            }
            Console.WriteLine("最高分是{0},是{1}考得", str[1], str[0]);



                Console.ReadLine();
打印最高分

二维数组替换

 1             Console.WriteLine("输入要替换的字");
 2             string c = Console.ReadLine();
 3             Console.WriteLine("输入要替换wei的字");
 4             string d = Console.ReadLine();
 5 
 6             string[,] array = new string[4, 6] { 
 7                { "","","","","",""},
 8                { "","","","","",""},
 9                { "","","","","",""},
10                { "","","","","",""}
11 
12             };
13             for (int i = 0; i < 4; i++)
14             {
15                 for (int j = 0; j < 6; j++)
16                 {
17                     if (array[i, j] == c)
18                     {
19                         array[i, j] = d;
20                     }
21                 }
22 
23             }
24             foreach (string aa in array)
25             {
26                 Console.Write(aa);
27             }
28 
29             Console.ReadLine();
替换诗句中的词

 

posted @ 2016-10-15 16:08  get("新技能")  阅读(238)  评论(0编辑  收藏  举报