二维数组、多维数组
1.二维数组
二维数组由多个一维数组组成,其定义方式:
1 int [,] array=new int [3,4]{ 2 {1,2,3,4}, 3 {3,4,5,6}, 4 {5,6,7,8} 5 };
二维数组中括号中,逗号左边表示一维数组的个数,也可以说控制行,逗号后面的数表示每个一维数组里面有几个元素,也可以说控制列。
1.输入班级人数,输入每个人的语数英成绩,并输出
1 Console.Write("请输入班级人数:"); 2 int a = int.Parse(Console.ReadLine()); 3 double[,] score = new double[a, 3]; 4 for (int i = 0; i < a; i++) 5 { 6 Console.Write("请输入第{0}个人的语文成绩:", i + 1); 7 score[i, 0] = double.Parse(Console.ReadLine()); 8 Console.Write("请输入第{0}个人的数学成绩:", i + 1); 9 score[i, 1] = double.Parse(Console.ReadLine()); 10 Console.Write("请输入第{0}个人的英语成绩:", i + 1); 11 score[i, 2] = double.Parse(Console.ReadLine()); 12 } 13 for (int i = 0; i < a; i++) 14 { 15 for (int j = 0; j < 3; j++) 16 { 17 Console.Write(score[i, j] + "\t"); 18 } 19 Console.WriteLine(); 20 }
输入班级人数,输入每个人的语数英成绩,求语文两个最高分 数学两个最低分 英语平均分
1 Console.Write("请输入班级人数:"); 2 int a = int.Parse(Console.ReadLine()); 3 double[,] score = new double[a, 3]; 4 for (int i = 0; i < a; i++) 5 { 6 Console.Write("请输入第{0}个人的语文成绩:", i + 1); 7 score[i, 0] = double.Parse(Console.ReadLine()); 8 Console.Write("请输入第{0}个人的数学成绩:", i + 1); 9 score[i, 1] = double.Parse(Console.ReadLine()); 10 Console.Write("请输入第{0}个人的英语成绩:", i + 1); 11 score[i, 2] = double.Parse(Console.ReadLine()); 12 } 13 for (int i = 0; i < a - 1; i++) 14 { 15 for (int j = i + 1; j < a; j++) 16 { 17 if (score[i, 0] < score[j, 0]) 18 { 19 double zhong = score[i, 0]; 20 score[i, 0] = score[j, 0]; 21 score[j, 0] = zhong; 22 zhong = score[i, 1]; 23 score[i, 1] = score[j, 1]; 24 score[j, 1] = zhong; 25 zhong = score[i, 2]; 26 score[i, 2] = score[j, 2]; 27 score[j, 2] = zhong; 28 } 29 } 30 } 31 Console.WriteLine("两个语文最高分是{0}、{1}", score[0, 0], score[1, 0]); 32 for (int i = 0; i < a - 1; i++) 33 { 34 for (int j = i + 1; j < a; j++) 35 { 36 if (score[i, 1] > score[j, 1]) 37 { 38 double zhong = score[i, 0]; 39 score[i, 0] = score[j, 0]; 40 score[j, 0] = zhong; 41 zhong = score[i, 1]; 42 score[i, 1] = score[j, 1]; 43 score[j, 1] = zhong; 44 zhong = score[i, 2]; 45 score[i, 2] = score[j, 2]; 46 score[j, 2] = zhong; 47 } 48 } 49 } 50 Console.WriteLine("两个数学最低分是{0}、{1}", score[0, 1], score[1, 1]); 51 double sum = 0; 52 for (int i = 0; i < a; i++) 53 { 54 sum += score[i, 2]; 55 } 56 Console.WriteLine("英语平均分是{0}", sum / a);
打印“王”字
1 string[,] wang = new string[7, 7] 2 { 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 }
split() 以。。。。进行分割
分割出来的内容需要放在string类型的数组当中不需要给数组定义长度
2.三维数组
定义方式:
int[, ,] array = new int[3, 2, 2];
一个元素是一个点
一维数组是一条线
二维数组是一个面(表格)
三维数组是一个体(立体)