【问题标题】:My input gets lost somewhere along the way(printing 0s)我的输入在途中丢失了(打印 0)
【发布时间】:2013-04-08 09:49:20
【问题描述】:

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

文本。文件目前看起来像这样,但将扩展为包含更多,大约 100 个左右。 900 只是用作上限。

21,f,s,14

41,f,m,22

12、m、s、12

11、f、s、8

29,米,米,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]);
        }
    }
}

当前输出如下所示:

我的数据在哪里丢失了?

【问题讨论】:

  • 你尝试调试它?
  • 这一行在您的while (i &lt; SIZE &amp;&amp; i &lt; 0) 方法中是否正确ReadFile
  • 快速将其插入Visual Studio,我可以看到上面的@Barry,确实是正确的。
  • 我把它改成了 (i 0) 还是一样的。不过还是谢谢你。
  • @Barry 问题在这一行。这永远不会返回 true。所以循环永远不会执行。

标签: c# arrays output


【解决方案1】:

问题出在这一行。

while (i < SIZE && i < 0)

这个循环永远不会执行。用这个替换它。

while (i < SIZE && i <= 0)
{
    foreach (string line in File.ReadAllLines("census.txt"))
    {
        if (!string.IsNullOrEmpty(line))
        {
            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++;
        }
    }
}

【讨论】:

  • 如果你擅长调试,你就会知道问题出在哪里。第二次执行循环时会崩溃。因为行将是空字符串。
  • 我是新手。我不确定这是不是可以在桌面检查或与 C# 相关的东西中发现的逻辑错误。
  • @Sabotenderizer,一次一个问题。为不同的问题创建一个新主题。他的回答奏效了吗?如果是这样,请接受它,继续前进,研究您的下一个问题,如果您遇到困难,请告诉我们。
  • 仅适用于年龄,即便如此,打印出年龄本身而不是每个年龄出现的次数。
【解决方案2】:

您的ReadFile 错误。试试这样吧。

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,您可以轻松地遍历每一行,而无需跟踪您所在的行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2013-12-25
    • 2010-10-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多