二维数组

//二维数组
int[,] second = new int[2,3]//有两个长度为三的一维数组
{
{3,2,5},
{6,7,8}
};
int[] shu=new int[3]{3,2,5};//一维数组中的三个数

second[0, 1] = 9;
second[1, 0] = 4;

//让数组横向显示
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3;j++ )
{
Console.Write(second [i,j]);
}
Console.Write("\n");
}

foreach (int a in second)
{
Console.WriteLine(a);
}
*/

//输入人数,姓名身高年龄,求平均年龄,身高降序排列
Console.Write("请输入人数:");
int n = int.Parse(Console.ReadLine());

string[,] shuzu = new string[n, 3];
decimal sum = 0;
for (int i = 0; i < n; i++)
{
Console.Write("姓名");
shuzu[i, 0] = Console.ReadLine();
Console.Write("年龄");
shuzu[i, 1] = Console.ReadLine();
sum += decimal.Parse(shuzu[i, 1]);
Console.Write("身高");
shuzu[i, 2] = Console.ReadLine();
}
decimal avg = sum / n;

for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (decimal.Parse(shuzu[i, 2]) < decimal.Parse(shuzu[j, 2]))
{
string zhong;
zhong = shuzu[i, 0];
shuzu[i, 0] = shuzu[j, 0];
shuzu[j, 0] = zhong;

string zhong1;
zhong1 = shuzu[i, 1];
shuzu[i, 1] = shuzu[j, 1];
shuzu[j, 1] = zhong1;

string zhong2;
zhong2 = shuzu[i, 2];
shuzu[i, 2] = shuzu[j, 2];
shuzu[j, 2] = zhong2;
}
}
}


for (int i = 0; i < n; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(shuzu[i, j] + " ");
}
Console.Write("\n");
}
Console .ReadLine();

posted @ 2014-12-18 10:19  巽坤  阅读(136)  评论(0编辑  收藏  举报