我正在创建一个创建“世界生成器”的业余爱好项目。'我试图用很多“人类”来填充我的世界。每个对象获得一个随机生成的名称。我有一个NameGenerator类,它有一个函数generateName,每次调用它时都应生成一个随机名。在我的世界里,我正在经历一个“日子”。每秒运行一次,将100个人添加到人员列表中。由于某种原因,当运行它时,很多人类对象会生成相同的名称,尽管它们应该有不同的名称。
这是我的代码:
mainGui.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JamiesWorldGenerator
{
public class World
{
private delegate void updateForm();
WorldGeneratorForm gui = null;
List<Country> worldCountries = new List<Country>();
List<Human> worldHumans = new List<Human>();
List<Human> newWorldHumans;
System.Timers.Timer timer = new System.Timers.Timer();
int daysElapsed = 0;
public World(WorldGeneratorForm mainGui, int dayDelay)
{
gui = mainGui;
timer.Interval = dayDelay;
timer.Elapsed += day;
}
private void day(object sender, EventArgs e)
{
newWorldHumans = new List<Human>();
for (int i = 0; i < 100; i++)
{
NameGenerator nameGen = new NameGenerator();
Human h = new Human(nameGen.generateName(4, 8), nameGen.generateName(4, 12));
worldHumans.Add(h);
newWorldHumans.Add(h);
}
daysElapsed++;
Delegate updateDays = new updateForm(updateDayElapsed);
Delegate updatePersonList = new updateForm(updateWorldPopulationList);
gui.Invoke(updateDays);
gui.Invoke(updatePersonList);
}
public void run()
{
timer.Enabled = true;
}
public void pause()
{
timer.Enabled = false;
}
private void updateDayElapsed()
{
gui.updateTimeElapsed(daysElapsed);
}
private void updateWorldPopulationList()
{
gui.updatePersonList(newWorldHumans);
}
}
}
mainGui.designer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
namespace JamiesWorldGenerator
{
public class Human
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public Human(string fname, string lname)
{
FirstName = capitaliseFirst(fname);
LastName = capitaliseFirst(lname);
FullName = FirstName + " " + LastName;
}
string capitaliseFirst(string s)
{
if (s.Length == 0) return "";
char[] chars = s.ToCharArray();
chars[0] = Char.ToUpperInvariant(chars[0]);
return new string(chars);
}
}
}
World.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JamiesWorldGenerator
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new WorldGeneratorForm());
}
}
}
Human.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JamiesWorldGenerator
{
class NameGenerator
{
Random random = new Random();
public string generateName(int minLength, int maxLength)
{
string chars = "abcdefghijklmnopqrstuvwxyz";
string consonants = "bcdfghjklmnpqrstvxzw";
string vowels = "aeiouy";
int length = random.Next(minLength, maxLength);
string name = "";
for (int i = 0; i < length; i++)
{
if (name != "")
{
if (endsWithTwoConsonants(name)) name = name + vowels[random.Next(vowels.Length)];
else if (endsWithTwoVowels(name)) name = name + consonants[random.Next(consonants.Length)];
else name = name + chars[random.Next(chars.Length)];
}
else name = name + chars[random.Next(chars.Length)];
}
return name;
}
bool endsWithTwoConsonants(string s)
{
if (s.Length < 2) return false;
string substring = s.Substring(s.Length - 2);
for(int i = 0; i < substring.Length; i++)
{
if (isVowel(substring[i])) return false;
}
return true;
}
bool endsWithTwoVowels(string s)
{
string[] suffixes = {"aa", "ae", "ai", "ao", "au", "ea", "ee", "ei", "eo", "eu", "ia", "ie", "ii", "io", "iu"
,"oa", "oe", "oi", "oo", "ou", "ua", "ue", "ui", "uo", "uu"};
for(int i = 0; i < suffixes.Length; i++)
{
if (s.EndsWith(suffixes[i])) return true;
}
return false;
}
bool isVowel(char c)
{
string vowels = "aeiouy";
if (vowels.Contains(c))
{
return true;
}
else
{
return false;
}
}
bool isConsonant(char c)
{
string consonants = "bcdfghjklmnpqrstvxzw";
if (consonants.Contains(c))
{
return true;
}
else
{
return false;
}
}
}
}
Program.cs的
.backgroundcolor1 {
background-color: #567892;
}
.back_imag{position:absolute !important; background:url(http://via.placeholder.com/500x400) no-repeat; background-size:cover; background-position:center center; left:0px; top:0px; bottom:0px;
}
编辑 - NameGenerator.cs 抱歉,我以为我发布了它(傻乎乎的)
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<section class="container">
<div class="row" align="center">
<div class="col-xs-12 backgroundcolor1">
<div class="col-xs-6 col-sm-6">
<div class="col-md-10 col-lg-offset-1 ">
<br>
<img src="assets/images/SPA/Icons/facials.png" class="icons" alt=""><br>
<h3>FACIALS</h3>
<p align="justify">Have you ever treated to a facial?<br> Facial treatments feel good, offering soothing relaxation, according to Day Spas. Facial massage treatments increase circulation and the flow of blood, and they include a variety of cremes,
aromatherapy and oils that moisturize the skin, smell good, and relieve stress as well as encourage peace of mind and contentment</p>
</div>
</div>
<div class="col-xs-6 col-sm-6 textbox back_imag col-xs-offset-6">
<img src="" class="" alt="">
<figcaption><br><br><br>
<h3>Facials</h3>
</figcaption>
[enter image description here][1]
</div>
</div>
</div>
</section>
任何帮助都会得到很大的帮助
答案 0 :(得分:4)
我将放置NameGenerator nameGen = new NameGenerator();世界外地的日期功能声明for循环将解决你的问题。
NameGenerator nameGen = new NameGenerator();
for (int i = 0; i < 100; i++)
{
Human h = new Human(nameGen.generateName(4, 8), nameGen.generateName(4, 12));
worldHumans.Add(h);
newWorldHumans.Add(h);
}
答案 1 :(得分:0)
使NameGenerator具有带有初始种子的单例随机,因此每次实例化NameGenerator并生成名称时,它总是不同的。
class NameGenerator
{
private static readonly Random staticRandom = new Random();
private Random random {get;set;}
public NameGenerator()
{
random = staticRandom;
}
...