数字输出全是零?

时间:2017-11-08 05:02:40

标签: c#

我想在一个包含人口普查数据的文件中阅读。根据这些数据,我需要输出在特定地区居住的人数以及特定年龄段的人数。

问题是我的输出显示全部为零,除了"年龄在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 . . .

2 个答案:

答案 0 :(得分:0)

您的int a方法需要out int avalidDataAge

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

答案 1 :(得分:0)

你所表现出的承诺;这是一个很好的学生初学者的努力。我不知道你会理解多少这些,但我希望你至少有机会看到更接近它的外观,以帮助你养成良好的习惯。

public class CensusRecord
{
    public int Age {get;set;}
    public int DistrictID {get;set;}
    public bool DataReady {get;private set;}

    public static CensusRecord FromDataLine(string line)
    {
       // I don't normally recommend .Split() for CSV data -- too many edge cases
       //  -- but this didn't seem like the time to pull in a CSV reader
       var fields = line.Split(','); 
       int age = 0, district = 0;

       var result = new CensusRecord();
       result.DataReady = true;

       if (int.TryParse(fieldsArray[0], out age))
           result.Age = age;
       else
           result.DataReady = false;

       if (int.TryParse(fieldsArray[3], out district))
           result.DistrictID = district;
       else
           result.DataReady = false;

        return result;          
    }
}

class Program
{
    static IEnumerable<CensusRecord> LoadData(string filename)
    {
        return File.ReadLines(filename)
             .Select(line => CensusRecord.FromDataLine(line))
             .Where(r => r.DataReady);
    }

    static void Main(string[] args)
    {
        var data = LoadData("censusdata.txt").ToList();

        DisplayDistrictPopulations(data);
        Console.WriteLine("");
        DisplayAgeGroups(data);

        Console.WriteLine("Press any key to continue...");
        Console.ReadKey(true);
    }

    static void DisplayDistrictPopulations(IEnumerable<CensusRecord> data)
    {
        foreach(var district in data.GroupBy(r => r.DistrictID).OrderBy(g => g.Key))
        {
             Console.WriteLine("District {0}: Population = {1}", district.Key, district.Count());
        }
    }

    static void DisplayAgeGroups(IEnumerable<CensusRecord> data)
    {
        var groups = data.GroupBy(r => {
            if (r.Age <  18) return 0;
            if (r.Age <= 30) return 1;
            if (r.Age <= 45) return 2;
            if (r.Age <= 64) return 3;
            return 4;
          }).OrderBy(g => g.Key);

        string[] ageStrings = {"Age Under 18", "Ages 18-30","Ages 31-45", "Ages 46-64", "65 & Over"};

        foreach (var age in groups)
        {
            Console.WriteLine("{0}: Population = {1}", ageStrings[age.Key], age.Count());
        }
    }
}