我的输入在途中某处丢失(打印0) [英] My input gets lost somewhere along the way(printing 0s)

查看:76
本文介绍了我的输入在途中某处丢失(打印0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试对包含以下内容的数据列表进行排序:

Trying to sort a list of data that contains the following:

txt.文件目前看起来像这样,但将扩展为包含更多(大约100个左右). 900只是一个上限.

The txt. file currently looks like this but will be extended to include more, about 100 or so. 900 is just used as an upper limit.

21,f,s,14

21,f, s, 14

41,f,m,22

41,f, m, 22

12,m,s,12

12, m, s, 12

11,f,s,8

29,m,m,4

6,m,s,12

9,f,s,2

30,f,s,1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class Program
{
    const int SIZE = 900;
    const int SIZEDISTRICT = 22;
    const int RANGE = 5;
    static void Main()
    {   
        //These arrays will hold the split data from a text file.
        int[] districtDataD = new int[900];
        string[] districtDataG = new string[900];
        string[] districtDataM = new string[900];
        int[] districtDataA = new int[900];

        //countDistrict will hold how many hypothetical people in each hypothetical district and 
        //ages will hold how many hypothetical people between certain hypothetical ages.
        int[] countDistrict = new int[SIZEDISTRICT];
        int[] ages = new int[RANGE] { 0, 18, 30, 45, 65};


        //Modules
        ReadFile(districtDataD, districtDataG, districtDataM,districtDataA);
        CountPopulation(districtDataD, countDistrict);
        AgeRanges(districtDataA, ages);
        DisplayData(countDistrict, districtDataA, ages);
    }//End Main


    //This module splits and inserts the data into the four first arrays
    static void ReadFile(int[] districtDataD, string[] districtDataG, string[] districtDataM, int[] districtDataA)
    {
        string[] lines = File.ReadAllLines("census.txt");
        int i = 0;

        while (i < SIZE  && i < 0)
        {
            foreach (string line in File.ReadAllLines("census.txt"))
            {
                string[] parts = line.Split(',');

                districtDataD[i] = int.Parse(parts[0]);
                districtDataG[i] = parts[1];
                districtDataM[i] = parts[2];
                districtDataA[i] = int.Parse(parts[3]);
                i++;
            }
        }
    }
    //This module counts how many hypothetical people are in each fictional district
   static void CountPopulation(int[] districtDataD, int[] countDistrict)
    {
        int i = 0;
        for (i = 0; i < districtDataD.Length; i++)
        {
            if (districtDataD[i] > 0 && districtDataD[i] < districtDataD.Length)
            {
                districtDataD[countDistrict[i]]
                    ++;
            }
        }
    }


    //This module sorts the ages into 0-18, 19-30, 31-45, 46-65, and 65 and up
     private static void AgeRanges(int[] districtDataA, int[] ages)
     {
         int idx = 0;
         for (idx = 0; idx < districtDataA.Length && ages[idx] > districtDataA[idx]; idx++)
         {

             ages[idx] = districtDataA[idx];
         }
     }


    //This module displays the data
     static void DisplayData(int[] countDistrict, int[] districtDataA, int[] ages)
    {
        int index = 0;
        for (index = 0; index < countDistrict.Length; index++)
        {
            Console.WriteLine(" District {0}: {1}", index + 1, countDistrict[index]);
        }

        int x = 0;
        for (x = 0; x < ages.Length; x++)
        {
            Console.WriteLine("Ages over {0} : {1}", ages[x], districtDataA[x]);
        }
    }
}

当前输出如下:

我的数据在哪里丢失?

推荐答案

您的ReadFile错误.尝试这样的事情.

Your ReadFile is wrong. Try something like this instead.

static void ReadFile(int[] districtDataD, string[] districtDataG, string[] districtDataM, int[] districtDataA)
{
   // Read in the file contents
    string[] lines = File.ReadAllLines("census.txt");
    int i =0;
    //iterate through each line
    foreach(var line in lines)
    {
      //split the line by a comma
      string[] parts = line.Split(',');

            districtDataD[i] = int.Parse(parts[0]);
            districtDataG[i] = parts[1];
            districtDataM[i] = parts[2];
            districtDataA[i] = int.Parse(parts[3]);
            i++;
    }

}

您当前正在读取文件,并将其分配给lines,然后再次读取文件,并将值分配给line.如果使用foreach,则可以轻松地遍历每一行,而不必跟踪您所在的行.

You are currently reading in the file and assigning it to lines then reading the file in again and assigning the value to line. If you use foreach you can easily loop through each line without having to keep track of which line you are on.

这篇关于我的输入在途中某处丢失(打印0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆