c# - 需要帮助设置带有这些 int 变量的数组

标签 c# arrays

我对长调用方法表示歉意,但我对 C# 仍然很陌生,并且想知道如何为我的变量设置数组,这样就不会浪费太多编码。该死的东西也不会读取包含“28,m,s,3”这样的行的文本文件

namespace project2
{
    class Program
    {
        static void Main(string[] args)
        {
            int livingIn = 0;
            int dist1 = 0;
            int dist2 = 0;
            int dist3 = 0;
            int dist4 = 0;
            int dist5 = 0;
            int dist6 = 0;
            int dist7 = 0;
            int dist8 = 0;
            int dist9 = 0;
            int dist10 = 0;
            int dist11 = 0;
            int dist12 = 0;
            int dist13 = 0;
            int dist14 = 0;
            int dist15 = 0;
            int dist16 = 0;
            int dist17 = 0;
            int dist18 = 0;
            int dist19 = 0;
            int dist20 = 0;
            int dist21 = 0;
            int dist22 = 0;

        int age = 0;
        int UnderEighteen = 0;
        int NineteenToThirty = 0;
        int ThirtyoneToFortyfive = 0;
        int FortysixToSixtyfour = 0;
        int SixtyfiveAndUp = 0;

        textfilereader(livingIn,age,dist1,dist2,dist3,dist4,dist5,dist6,dist7,dist8,dist9,dist10,dist11,dist12,dist13,dist14,dist15,dist16,dist17,dist18,dist19,dist20,dist21,dist22,UnderEighteen,NineteenToThirty,ThirtyoneToFortyfive,FortysixToSixtyfour,SixtyfiveAndUp);
    }

    static void textfilereader(int livingIn, int dist1, int dist2, int dist3, int dist4, int dist5, int dist6, int dist7, int dist8, int dist9, int dist10, int dist11, int dist12, int dist13, int dist14, int dist15, int dist16, int dist17, int dist18, int dist19, int dist20, int dist21, int dist22, int age, int UnderEighteen, int NineteenToThirty, int ThirtyoneToFortyfive, int FortysixToSixtyfour, int SixtyfiveAndUp)
    {
        FileStream fstream = new FileStream("project2.txt", FileMode.Open, FileAccess.Read);
        StreamReader infile = new StreamReader(fstream);
        string inputrecord = "";
        string[] fields;

        inputrecord = infile.ReadLine();
        while (inputrecord != null)
        {
            fields = inputrecord.Split(',');
            if (int.TryParse(fields[0], out age))
            {
                agegroupcounter(age,UnderEighteen,NineteenToThirty,ThirtyoneToFortyfive,FortysixToSixtyfour,SixtyfiveAndUp);
            }
            if (int.TryParse(fields[3], out livingIn))
            {
                districtCounter(livingIn,dist1, dist2, dist3, dist4, dist5, dist6, dist7, dist8, dist9, dist10, dist11, dist12, dist13, dist14, dist15, dist16, dist17, dist18, dist19, dist20, dist21, dist22);
            }
            inputrecord = infile.ReadLine();
        }

        Finalcount(dist1,dist2,dist3,dist4,dist5,dist6,dist7,dist8,dist9,dist10,dist11,dist12,dist13,dist14,dist15,dist16,dist17,dist18,dist19,dist20,dist21,dist22,UnderEighteen,NineteenToThirty,ThirtyoneToFortyfive,FortysixToSixtyfour,SixtyfiveAndUp);

    }

    static void Finalcount(int dist1, int dist2, int dist3, int dist4, int dist5, int dist6, int dist7, int dist8, int dist9, int dist10, int dist11, int dist12, int dist13, int dist14, int dist15, int dist16, int dist17, int dist18, int dist19, int dist20, int dist21, int dist22, int UnderEighteen, int NineteenToThirty, int ThirtyoneToFortyfive, int FortysixToSixtyfour, int SixtyfiveAndUp)
    {
        Console.WriteLine("The age group tallies are");
        Console.WriteLine(" Eighteen and under = {0}", UnderEighteen);
        Console.WriteLine(" Nineteen to thirty = {0}", NineteenToThirty);
        Console.WriteLine(" Thirtyone to fortyfive = {0}", ThirtyoneToFortyfive);
        Console.WriteLine(" fortysix to sixtyfour = {0}", FortysixToSixtyfour);
        Console.WriteLine("Sixtyfive and older = {0}", SixtyfiveAndUp);
        Console.WriteLine("");

        Console.WriteLine("The total of persons living in each district");
        Console.WriteLine("Distict 1 = {0}",dist1);
        Console.WriteLine("Distict 2 = {0}", dist2);
        Console.WriteLine("Distict 3 = {0}", dist3);
        Console.WriteLine("Distict 4 = {0}", dist4);
        Console.WriteLine("Distict 5 = {0}", dist5);
        Console.WriteLine("Distict 6 = {0}", dist6);
        Console.WriteLine("Distict 7 = {0}", dist7);
        Console.WriteLine("Distict 8 = {0}", dist8);
        Console.WriteLine("Distict 9 = {0}", dist9);
        Console.WriteLine("Distict 10 = {0}", dist10);
        Console.WriteLine("Distict 11 = {0}", dist11);
        Console.WriteLine("Distict 12 = {0}", dist12);
        Console.WriteLine("Distict 13 = {0}", dist13);
        Console.WriteLine("Distict 14 = {0}", dist14);
        Console.WriteLine("Distict 15 = {0}", dist15);
        Console.WriteLine("Distict 16 = {0}", dist16);
        Console.WriteLine("Distict 17 = {0}", dist17);
        Console.WriteLine("Distict 18 = {0}", dist18);
        Console.WriteLine("Distict 19 = {0}", dist19);
        Console.WriteLine("Distict 20 = {0}", dist20);
        Console.WriteLine("Distict 21 = {0}", dist21);
        Console.WriteLine("Distict 22 = {0}", dist22);
    }


    static void districtCounter(int livingIn,int dist1,int dist2,int dist3,int dist4,int dist5,int dist6,int dist7,int dist8,int dist9,int dist10,int dist11,int dist12,int dist13,int dist14,int dist15,int dist16,int dist17,int dist18,int dist19,int dist20,int dist21,int dist22)
    { 
        if (livingIn <= 0 && livingIn >= 23)
        {
            cancel();
        }
        else
        if (livingIn == 1)
        {
            dist1 = dist1 + 1;
        }
        else
        if (livingIn == 2)
        {
            dist2 = dist2 + 1;
        }
        else
        if (livingIn == 3)
        {
            dist3 = dist3 + 1;
        }
        else
        if (livingIn == 4)
        {
            dist4 = dist4 + 1;
        }
        else
        if (livingIn == 5)
        {
            dist5 = dist5 + 1;
        }
        else
        if (livingIn == 6)
        {
            dist6 = dist6 + 1;
        }
        else
        if (livingIn == 7)
        {
            dist7 = dist7 + 1;
        }
        else
        if (livingIn == 8)
        {
            dist8 = dist8 + 1;
        }
        else
        if (livingIn == 9)
        {
            dist9 = dist9 + 1;
        }
        else
        if (livingIn == 10)
        {
            dist10 = dist10 + 1;
        }
        else
        if (livingIn == 11)
        {
            dist11 = dist11 + 1;
        }
        else
        if (livingIn == 12)
        {
            dist12 = dist12 + 1;
        }
        else
        if (livingIn == 13)
        {
            dist13 = dist13 + 1;
        }
        else
        if (livingIn == 14)
        {
            dist14 = dist14 + 1;
        }
        else
        if (livingIn == 15)
        {
            dist15 = dist15 + 1;
        }
        else
        if (livingIn == 16)
        {
            dist16 = dist16 + 1;
        }
        else
        if (livingIn == 17)
        {
            dist17 = dist17 + 1;
        }
        else
        if (livingIn == 18)
        {
            dist18 = dist18 + 1;
        }
        else
        if (livingIn == 19)
        {
            dist19= dist19 + 1;
        }
        else
        if (livingIn == 20)
        {
            dist20 = dist20 + 1;
        }
        else
        if (livingIn == 21)
        {
            dist21 = dist21 + 1;
        }
        else
        {
            dist22 = dist22 + 1;
        }
    }
    static void agegroupcounter(int age, int UnderEighteen, int NineteenToThirty, int ThirtyoneToFortyfive, int FortysixToSixtyfour, int SixtyfiveAndUp)
    {
        if (age < 0)
        {
            cancel();
        }
        else
            if (age <= 18)
        {
            UnderEighteen = UnderEighteen + 1;
        }
        else
            if (age >= 19 || age <= 30)
        {
            NineteenToThirty = NineteenToThirty + 1;
        }
        else
            if (age >=31 || age <= 45)
        {
            ThirtyoneToFortyfive = ThirtyoneToFortyfive + 1;
        }
        else
            if (age >=46 || age <= 64)
        {
            FortysixToSixtyfour = FortysixToSixtyfour + 1;
        }
        else
        {
            SixtyfiveAndUp = SixtyfiveAndUp + 1;
        }
    }

    static void cancel()
    {
        Console.WriteLine("Invalid Data Was Entered please check your text file for any irregularities");
    }
}

}

文本文件数组已提供给我,但某些内容未正确使用,因为计数方法不会累加并在最终计数方法中保持为零,在该方法中我显示了文本文件中包含的结果。

最佳答案

正如 Rob 在评论中建议的那样 " there's not much we can do to help without doing a complete re-write of your code"但我想通过提示来指导您更多地简化代码:

  • 使用int[]代替 dist1dist22并将其设置为全局,以便您可以避免传递数组并维护其计数。

    public static int[] dist = new int[22] ;  // use Enumerable.Repeat(x, 22).ToArray(); where x is an integer
    // This will give you an integer array, which are all initialized with 0
    
  • 使用Dictionary<int,int>保留年龄组,以组的最大数量作为键,最初计数为 0。可能如下所示:

    public static Dictionary<int,int> AgeGroups = new Dictionary<int,int>();
    // initialize
    AgeGroups.Add(18, 0); // for age group under 18
    AgeGroups.Add(30, 0);// for age group under 30
    AgeGroups.Add(40, 0);
    AgeGroups.Add(50, 0);
    AgeGroups.Add(60, 0);
    

这样就可以重写函数agegroupcounter如下所示:

static void agegroupcounter(int age)
   {
      if (age < 0)
       {
           cancel();
       }
       else
       {
          var index= AgeGroups.FirstOrDefault(x => x.Key < age).Key ;
          AgeGroups[index] += 1; 
       }                
   }

districtCounter 的签名将是:

 static void districtCounter(int livingIn)
    {
        if (livingIn <= 0 && livingIn >= 23)
        {
            cancel();
        }
        else
            dist[livingIn] += 1;
    }

最后是Finalcount将看起来像:

 static void Finalcount()
    {
        Console.WriteLine("The age group tallies are");
        foreach (var item in AgeGroups )
        {
            Console.WriteLine(" under {0} = {1}", item.Key,item.Value);
        }      

        Console.WriteLine("The total of persons living in each district");
        foreach (int d in dist)
        {  
            Console.WriteLine("Distict {0} = {1}", d,d);                
        } 
    }

注意:希望这些技巧能够帮助您简化代码,并将它们集成到代码中。祝你有美好的一天

关于c# - 需要帮助设置带有这些 int 变量的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563905/

相关文章:

c# - 如何将 IntPtr/Int 转换为 Socket?

java - 将数组中的每个元素向下移动一个 (Java)

javascript - 循环遍历数组并返回data属性值

c# - 使用 StreamReader.EndOfStream 的问题?

c# - 我应该如何过滤绑定(bind)到网格的 IReactiveList?

c# - Visual Studio 调试器挂起,无法从 w3wp 分离

c# - 在结对编程时是否可以使用任何基本的字符串混淆?

c++ - 是否可以在 C++ 中使用初始化列表设置数组的特定成员?

C# - 来自字节数组的 BigInteger 与来自 UInt64 的 BigInteger 产生不同的结果

java - 在for循环java中为json对象添加值