二维数组:
定义二维数组 int[,] myArray = new int[几个一维数组,数组中的个数];
数组可以具有多个维度。例如,下列声明创建一个四行两列的二维数组(可以理解为4个1维数组,数组中包含2个元素):
int[,] myArray = new int[4,2];
int[,] myArray = new int[4,2] {{1,2}, {3,4}, {5,6}, {7,8}};
取值则是
Int i=myArray[0,0]
输出结果i为:1
Int i=myArray[0,1]
输出结果i为:2
多维数组:
数组可以具有多个维度。例如:
int[,,] myArray = new int[2,4,2] {{1,2},{3,4},{5,6},{7,8}} , {{9,10},{11,12},{13,14},{15,16}};
一个元素是一个点 一维数组是一条线 二维数组一个面(表格) 三维数组是一个教学楼(立体)
split()
以***进行分割
分割开的内容需要放置在string类型的数组中,不需要给数组定义长度
Console.Write("请输入姓名-年龄-工作单位:");//"张三-33-汉企" string s = Console.ReadLine() ; string[] array = s.Split('-'); foreach(string aa in array) {Console.WriteLine(aa);}
上面是一位数组的一个题,前面做过,这次用的是一个数组!!
输入班级人数,输入每个人的语数英成绩求语文两个最高分,数学两个最低分,英语平均分
Console.Write("请输入班级人数:");int a = int.Parse(Console.ReadLine());double[,] s = new double[a, 3];for (int i = 0; i < a; i++){Console.Write("请输入第{0}个人的语文成绩:", (i + 1));s[i, 0] = double.Parse(Console.ReadLine());Console.Write("请输入第{0}个人的数学成绩:", (i + 1));s[i, 1] = double.Parse(Console.ReadLine());Console.Write("请输入第{0}个人的英语成绩:", (i + 1));s[i, 2] = double.Parse(Console.ReadLine());}for (int i = 0; i < a; i++){for (int j = 0; j < 3; j++){Console.Write(s[i, j] + "\t");}Console.WriteLine();}for (int i = 0; i < a - 1; i++){for (int j = i + 1; j < a; j++){if (s[i, 0] < s[j, 0]){double z = s[i, 0];s[i, 0] = s[j, 0];s[j, 0] = z;double z1 = s[i, 1];s[i, 1] = s[j, 1];s[j, 1] = z1;double z2 = s[i, 2];s[i, 2] = s[j, 2];s[j, 2] = z1;}}}Console.WriteLine("语文最高分为:" + s[0, 0] + "," + s[1, 0]);for (int i = 0; i < a - 1; i++){for (int j = i + 1; j < a; j++){if (s[i, 1] > s[j, 1]){double z = s[i, 0];s[i, 0] = s[j, 0];s[j, 0] = z;double z1 = s[i, 1];s[i, 1] = s[j, 1];s[j, 1] = z1;double z2 = s[i, 2];s[i, 2] = s[j, 2];s[j, 2] = z1;}}}Console.WriteLine("数学最低分为:" + s[0, 1] + "," + s[1, 1]);double sum = 0;for (int i = 0; i < a; i++){sum += s[i, 2];}Console.WriteLine("英语平均分为:" + sum / a);Console.ReadLine();
注意冒泡循环中,随着想要得到的分数互换的同时,也要把同一数组的其他项也互换,不然会影响其他结果。
查找替换问题
Console.Write("春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。\n");Console.Write("请输入要替换的文字:");string a = Console.ReadLine();Console.Write("请输入要替换为的文字:");string b = Console.ReadLine();string[] s = new string[24] { "春", "眠", "不", "觉", "晓", ",", "处", "处", "闻", "啼", "鸟", "。", "夜", "来", "风", "雨", "声", ",", "花", "落", "知", "多", "少", "。" };for (int i = 0; i < 24; i++){if (s[i] == a){s[i] = b;}}Console.Write("替换成功,替换后为:");foreach (string aa in s){Console.Write(aa);}Console.ReadLine();
string a = "春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。";Console.Write("请输入要替换的字:");string b = Console.ReadLine();Console.Write("请输入替换为的文字:");string c = Console.ReadLine();Console.WriteLine(a.Replace(b, c));Console.ReadLine();