初始化
int[] input = new int[] { 5, 10, 20, 35, 50, 68 };
int[,] ageGroups = new int[5, input.Length];
int[] ageCounters = new int[5];
分组
foreach(int age in input)
{
if(age >= 0 && age < 18)
{
ageGroups[0, ageCounters[0]] = age;
ageCounters[0]++;
}
else if(age >= 18 && age <= 30)
{
ageGroups[1, ageCounters[1]] = age;
ageCounters[1]++;
}
else if(age >= 31 && age <= 45)
{
ageGroups[2, ageCounters[2]] = age;
ageCounters[2]++;
}
else if(age >= 46 && age <= 64)
{
ageGroups[3, ageCounters[3]] = age;
ageCounters[3]++;
}
else if(age >= 65)
{
ageGroups[4, ageCounters[4]] = age;
ageCounters[4]++;
}
}
显示
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < ageCounters[i]; j++)
{
Console.Write(ageGroups[i, j] + " ");
}
Console.WriteLine();
}