我想读入一个包含人口普查数据的文件。根据这些数据,我需要输出给定地区的人口数量以及特定年龄段的人口数量。
问题是我的输出显示全为零,除了“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 = 0District 2: Population = 0District 3: Population = 0District 4: Population = 0District 5: Population = 0District 6: Population = 0District 7: Population = 0District 8: Population = 0District 9: Population = 0District 10: Population = 0District 11: Population = 0District 12: Population = 0District 13: Population = 0District 14: Population = 0District 15: Population = 0District 16: Population = 0District 17: Population = 0District 18: Population = 0District 19: Population = 0District 20: Population = 0District 21: Population = 0District 22: Population = 0Age Under 18: Population = 100Ages 18-30: Population = 0Ages 31-45: Population = 0Ages 46-64: Population = 065 & Over: Population = 0Press any key to continue . . .
您需要在 validDataAge
方法中将 int a
设为 out int a
:
static bool validDataAge(string[] fieldsArray, string inputData, out int a, bool age)
{
if (int.TryParse(fieldsArray[0], out a))
{
age = true;
return age;
}
else
{
age = false;
return age;
}
}
但是,作为进一步的建议,该方法可以写成:
static bool validDataAge(string inputData, out int a)
{
return int.TryParse(inputData, out a);
}
但是,validDataAge
与 int.TryParse
的功能基本相同,因此您可以完全删除 validDataAge
。
我是一名优秀的程序员,十分优秀!