二维数组,多维数组
//定义二维数组
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);
//打印“马”这个字
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 }
//一维数组是一条线,二维数组是一个面(表格)
//三维数组是一个立体(教学楼)
//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 }