随机名称生成器返回奇怪的结果

时间:2017-10-08 18:33:57

标签: c#

我目前正在开发随机名称凭据生成器。这是我的测试代码:

using FileHelpers;
using System;
using System.Collections.Generic;
using System.IO;

class Person
{

    public static List<String> snList = new List<string>();
    public static List<String> fnList = new List<string>();

    static bool isLoaded = false;

    private string _surname;
    private string _forename;
    private long _pesel;
    private int _phonenum;

    public string Surname { get => _surname; set => _surname = value; }
    public string Forename { get => _forename; set => _forename = value; }
    public long Pesel { get => _pesel; set => _pesel = value; }
    public int PhoneNum { get => _phonenum; set => _phonenum = value; }

    public Person()
    {
        if (isLoaded == false)
        {
            FileHelperEngine<Name> snEngine = new FileHelperEngine<Name>();
            string path = Path.Combine(Environment.CurrentDirectory, 
                                                      "firstnamescsv.csv");
            var read = snEngine.ReadFile(path);
            foreach (Name item in read)
            {
                snList.Add(item.Names);
            }

            FileHelperEngine<Name> fnEngine = new FileHelperEngine<Name>();
            path = Path.Combine(Environment.CurrentDirectory, "lastnamescsv.csv");
            var read1 = fnEngine.ReadFile(path);
            foreach (Name item in read)
            {
                fnList.Add(item.Names);
            }
            isLoaded = true;
        }

        PickSurname();
        PickForename();
        PickPesel();
        PickPhonenum();
    }

    public void PickSurname()
    {
        var rand = new Random();

        var pickedRand = rand.Next(1,5493);
        Surname = snList[pickedRand];
    }

    public void PickForename()
    {
        var rand1 = new Random();
        var random = rand1.Next(1,88777);
        Forename = fnList[random];
    }

    public void PickPhonenum()
    {
        var rand = new Random();
        PhoneNum = rand.Next(500000000, 799999999);
    }

    public void PickPesel()
    {
        var rand = new Random();
        Pesel = rand.Next(100000000, 999999999);
    }

    public override string ToString()
    {
        return $"Name: {Surname} {Forename}\n" +
            $"Phone number: {PhoneNum}\n" +
            $"Pesel: {Pesel}";
    }
}

名称等级

[FileHelpers.DelimitedRecord("\n")] 
class Name {
    private string _names;
    public string Names
    {
        get => _names;
        set => _names = value;
    }
 }

问题在于下面的输出:

  

姓名:Felicidad Garrels电话号码:595204613 Pesel:385613840
  姓名:Felicidad Garrels电话号码:595204613 Pesel:385613840
  姓名:Felicidad Garrels电话号码:595204613 Pesel:385613840
  姓名:Felicidad Garrels电话号码:595204613 Pesel:385613840
  姓名:Felicidad Garrels电话号码:595204613 Pesel:385613840

它是由这个Program.cs类引起的: (主)

 var list = new List<Person>();
        for (i = 0; i < 5 ; i++)
        {
            list.Add(new Person());
        }
        foreach(var item in list)
        {
            Console.WriteLine(item);
        }

0 个答案:

没有答案