數組格式數組
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();
二維數組ide
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類型的數組中,不須要給數組定義長度spa
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 }
打印圖形3d
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 }
一維數組例題code
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();
二維數組替換blog
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();