在数组开始C#之前显示唯一编号

时间:2018-02-24 23:43:16

标签: c# arrays

我正在开发一个小程序,它基本上从txt读取多个数组并将它们写入另一个文件,但另外,它应该生成一个唯一的数字并将它放在信息之前。我的第一部分工作没有问题,但第二部分导致我的问题,即使它应该工作。

    public static void Main(string[] args)
    {

        StreamReader vehiclereader = new StreamReader(@"C:\Users\Admin\Desktop\Program\vehicles.txt");
        string line = vehiclereader.ReadToEnd();

        string ID;
        string category;

        string Type;
        string Brand;
        string Model;
        string Year;
        string Colour;

        while (line != null)
        {
            var parts = line.Split(',');
            Type = parts[0];
            Brand = parts[1];
            Model = parts[2];
            Year = parts[3];
            Colour = parts[4];

            Console.WriteLine(line);

            string[] lines = { line };
            System.IO.File.WriteAllLines(@"C:\Users\Admin\Desktop\Program\vehicles2.txt", lines);

            List<string> categories = new List<string>();


            categories.Add(Type);
            int count = categories.Where(x => x.Equals(Type)).Count(); 
            ID = Type.Substring(0, 4) + count.ToString("00");

            Console.ReadKey();
        }
    }
}

目前,此代码从txt文件中读取,将其显示在控制台中并将其写回另一个txt文件。这很好,不愿意工作的部分是唯一的数字生成器。

最后5行代码应该在&#39; Type&#39;之前添加一个唯一的数字。数据总是从001开始。如果&#39; Type&#39;数据是相同的,然后数字按升序增长。否则,应为新类型重置唯一编号,并从001开始计数,并且对于相同类型应保持增长。 (例如,对于所有轻型车辆,计数器应该是相同的,对于重型车辆,计数器应该是不同的,但计算所有重型车辆)

我愿意接受任何帮助或建议!

1 个答案:

答案 0 :(得分:0)

此代码存在各种问题和建议,允许我在提供更正版本之前列出它们:

  1. StreamReader是一次性的,所以把它放在“使用”块中。
  2. ReadToEnd将整个文件读入单个字符串,而您的代码结构期望它一次返回一行,因此您需要“ReadLine”方法。
  3. 在你的循环中不会修改line的值,因此你将得到一个无限循环(程序永远不会结束)。
  4. (建议)在变量名称的开头使用小写字母,它将帮助您发现变量是什么以及类/方法是什么。
  5. (建议)声明局部变量的范围比需要的范围更广。只在循环中声明它们没有性能损失,它使您的程序更容易阅读。
  6. “string [] lines = {line};”命名意味着您认为这会将单行拆分为一个行数组。但实际上,它只会创建一个包含一个项目的数组(我们已经建立了该文件的全部内容)。
  7. “category”是未使用的变量;但实际上,您也不使用品牌,型号,年份或颜色。
  8. 如果问题有几行作为输入和输出的例子,那会有所帮助。
  9. 既然你一次处理一行,我们也可以一次写一行输出文件,而不是一次把整个文件保存在内存中。
  10. 该ID未使用,该代码位于写入输出文件的行之后,因此无法在其中显示。
  11. “int count = categories.Where(x =&gt; x.Equals(type))。Count();”是低效的,因为它遍历列表两次:首选“int count = categories.Count(x =&gt; x.Equals(type));”
  12. 删除了“Console.Write”,因为输出转到了文件。
  13. “Console.ReadKey”是指在循环内还是在循环之后?我把它放在外面。
  14. 我创建了一个负责计数的课程,以展示如何“分离关注点”。

    显然我没有你的文件,所以我不知道这是否有用。

    class Program
    {
        public static void Main(string[] args)
        {
            var typeCounter = new TypeCounter();
    
            using (StreamWriter vehicleWriter = new StreamWriter(@"C:\Users\Admin\Desktop\Program\vehicles2.txt"))
            using (StreamReader vehicleReader = new StreamReader(@"C:\Users\Admin\Desktop\Program\vehicles.txt"))
            {
                string line;
                while ((line = vehicleReader.ReadLine()) != null)
                {
                    var parts = line.Split(',');
                    string type = parts[0].Substring(0, 4); // not sure why you're using substring, I'm just matching what you did
                    var identifier = typeCounter.GetIdentifier(type);
                    vehicleWriter.WriteLine($"{identifier},{line}");
                }
            }
    
            Console.ReadKey();
        }
    }
    
    public class TypeCounter
    {
        private IDictionary<string, int> _typeCount = new Dictionary<string, int>();
    
        public string GetIdentifier(string type)
        {
            int number;
            if (_typeCount.ContainsKey(type))
            {
                number = ++_typeCount[type];
            }
            else
            {
                number = 1;
                _typeCount.Add(type, number);
            }
    
            return $"{type}{number:00}"; // feel free to use more zeros
        }
    }