1

私はプログラミングにかなり慣れていないため、次のプログラム コードをモデル化するのに苦労しています。

プログラムはファイルを読み取り、特定の要件を選択して表示します。教科書に示されているように、渡す配列を引数または関数として使用しようとしましたが、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));
4

1 に答える 1