我对编程相当陌生,并且难以对以下程序代码进行建模。
该程序读取一个文件,选择某些需求并显示它们。已尝试使用传递数组作为我的教科书中所述的参数或函数,但我似乎无法将其正确写入 C#。使用 int getAges(int array[], integer) 等示例。
//Parsing data into memory
string content;
using (StreamReader reader = new StreamReader(File.Open("Data.txt", FileMode.Open)))
{
content = reader.ReadToEnd();
}
string[] rows = content.Split('\n'); //Each row is on a new line
string[][] table = new string[rows.Length][];
for (int i = 0; i < rows.Length; table[i] = rows[i].Split(','), i++) ;
//selecting information
int[] districts = new int[rows.Length];
int[] ages = new int[rows.Length];
for (int i = 0; i < rows.Length; i++)
{
districts[i] = int.Parse(table[i][3]);
ages[i] = int.Parse(table[i][0]);
}
//Analyzing selected information
foreach (int district in districts.Distinct().OrderBy(x => x))
Console.WriteLine("District {0} has {1} resident(s)", district, districts.Count(x => x == district));
Console.WriteLine("Ages 0-18 : {0} resident(s)", ages.Count(x => x < 18));
Console.WriteLine("Ages 18-30 : {0} resident(s)", ages.Count(x => x >= 18 && x <= 30));
Console.WriteLine("Ages 31-45 : {0} resident(s)", ages.Count(x => x >= 31 && x <= 45));
Console.WriteLine("Ages 46-64 : {0} resident(s)", ages.Count(x => x >= 46 && x <= 64));
Console.WriteLine("Ages >=65 : {0} resident(s)", ages.Count(x => x >= 65));