9、C#根底整理(多維數組)。本站提示廣大學習愛好者:(9、C#根底整理(多維數組))文章只能為提供參考,不一定能成為您想要的結果。以下是9、C#根底整理(多維數組)正文
參考頁面:
http://www.yuanjiaocheng.net/CSharp/Csharp-while-loop.html
http://www.yuanjiaocheng.net/CSharp/Csharp-do-while-loop.html
http://www.yuanjiaocheng.net/CSharp/Csharp-structure.html
http://www.yuanjiaocheng.net/CSharp/Csharp-enum.html
http://www.yuanjiaocheng.net/CSharp/Csharp-stringbuilder.html
多維數組 1、二維數組:表示辦法:
int[y,x],x、y是索引,y代表行,x代表列。
例:
int[,] second = new int[2, 3]{
{3,2,5},
{6,7,8}
};//{}可以不寫
修正辦法:
second[0, 1] = 3;//表示將第0行第1列的數字改為3
練習:用二維數組停止冒泡排序:
輸出人數,輸出每團體的年齡、身高、姓名,求均勻年齡,按身高從高到低排序
Console.WriteLine("請輸出人數:");
int n = int.Parse(Console.ReadLine());
string[,] ren = new string[n, 3];
//辨別錄入每個學生的信息
for (int i = 0; i < n; i++)
{
Console.WriteLine("請輸出姓名、年齡、身高,用回車鍵分隔:");
for(int j = 0 ;j<3;j++)
{
ren[i, j] = Console.ReadLine();
}
}
double sum = 0;
//計算總年齡,打印均勻年齡
for(int i = 0;i<n;i++)
{
sum = sum +int.Parse(ren[i,1]);
}
Console.WriteLine("均勻年齡為:{0}",Math.Floor(sum/n));
Console.WriteLine("姓名 年齡 身高");
//依據身高停止排序
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
if (int.Parse(ren[j, 2]) > int.Parse(ren[i, 2]))
{
string[] zhong = {ren[j,0],ren[j,1],ren[j,2]};
//交流一切信息,使身高的排序與姓名、年齡堅持分歧
ren[j, 0] = ren[i, 0];
ren[j, 1] = ren[i, 1];
ren[j, 2] = ren[i, 2];
ren[i, 0] = zhong[0];
ren[i, 1] = zhong[1];
ren[i, 2] = zhong[2];
}
}
}
int [,] ab = new int[0,0];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(ren[i, j]+" ");
}
Console.Write("\n");
}
*2、多維數組
寫法:int[z,y,x]:z表示有幾個二維數組,運用辦法同二維數組