国勢調査データからファイルを読み込みたいです。このデータから、特定の地区に住んでいる人の数と特定の年齢層内にいる人の数を出力する必要があります。数値出力はすべてゼロですか?
問題は、「Age Under 18」行が100を示していることを除いて、すべての出力がすべてゼロになっています。参考のため、読み込まれるファイルには100人と22の地区があります。
はここに私のコードです:
class Program
{
static void Main(string[] args)
{
FileStream fStream = new FileStream("censusdata.txt", FileMode.Open, FileAccess.Read);
StreamReader inFile = new StreamReader(fStream);
string input = "";
int age = 0;
int district = 0;
const int AGE = 5;
const int DISTRICT = 22;
string[] fields;
bool ageBool = true;
bool distBool = true;
int[] ageCount = new int[AGE];
int[] ageRange = new int[AGE] { 0, 18, 30, 45, 64 };
int[] districtCount = new int[DISTRICT];
int[] districtRange = new int[DISTRICT];
input = inFile.ReadLine();
while(input != null)
{
fields = input.Split(',');
input = inFile.ReadLine();
if (ageBool)
{
validDataAge(fields, input, age, ageBool);
getValuePerAge(age, ageCount, ageRange);
}
else
Console.WriteLine("error");
if (distBool)
{
validDataDistrict(fields, input, district, distBool);
getValuePerDistrict(district, districtCount, districtRange);
}
else
Console.WriteLine("error");
}
displayOutput(districtCount, ageCount);
}
static bool validDataAge(string[] fieldsArray, string inputData, int a, bool age)
{
if (int.TryParse(fieldsArray[0], out a))
{
age = true;
return age;
}
else
{
age = false;
return age;
}
}
static bool validDataDistrict(string[] fieldsArray, string inputData, int d, bool district)
{
if (int.TryParse(fieldsArray[3], out d))
{
district = true;
return district;
}
else
{
district = false;
return district;
}
}
static void getValuePerDistrict(int d, int[] districtCountArray, int[] districtRangeArray)
{
for (int x = 1; x <= districtRangeArray.Length; x++)
{
if (x == d)
districtCountArray[x]++;
}
}
static void getValuePerAge(int a, int[] ageCountArray, int[] ageRangeArray)
{
int index = ageRangeArray.Length - 1;
while(a < ageRangeArray[index])
index--;
ageCountArray[index]++;
}
public static void displayOutput(int[] districtCountArray, int[] ageCountArray)
{
for (int x = 0; x < districtCountArray.Length; x++)
{
Console.WriteLine("District " + (x + 1) + ": Population = " + districtCountArray[x]);
}
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("Age Under 18: Population = " + ageCountArray[0]);
Console.WriteLine("Ages 18-30: Population = " + ageCountArray[1]);
Console.WriteLine("Ages 31-45: Population = " + ageCountArray[2]);
Console.WriteLine("Ages 46-64: Population = " + ageCountArray[3]);
Console.WriteLine("65 & Over: Population = " + ageCountArray[4]);
}
}
をそしてここに出力されます。
District 1: Population = 0 District 2: Population = 0 District 3: Population = 0 District 4: Population = 0 District 5: Population = 0 District 6: Population = 0 District 7: Population = 0 District 8: Population = 0 District 9: Population = 0 District 10: Population = 0 District 11: Population = 0 District 12: Population = 0 District 13: Population = 0 District 14: Population = 0 District 15: Population = 0 District 16: Population = 0 District 17: Population = 0 District 18: Population = 0 District 19: Population = 0 District 20: Population = 0 District 21: Population = 0 District 22: Population = 0 Age Under 18: Population = 100 Ages 18-30: Population = 0 Ages 31-45: Population = 0 Ages 46-64: Population = 0 65 & Over: Population = 0 Press any key to continue . . .
ここで、 'censusdata.txt'> – zaitsman
は、私のプロジェクトのデバッグフォルダに保存されています。基本的には次のようになります: – npeters
72、M、S、10 ..年齢は72歳、地理的区域は10です。 – npeters