Clan因为我没有定义这个,所以我不能用这个类来做这个。因此,如果您不介意,我将使用List<string>它,但此时Clan必须是一个集合:)
我们将使用RandomandList<string>来解决这个问题。
首先,我们的集合中必须有一些项目List<string>。让我们List<string>首先使用以下代码创建
List<string> Clans = new List<string>();
然后,让我们添加一些项目到Clans
Clans.Add("Uchiha,Konoha");
Clans.Add("Picrofo Groups,Egypt");
Clans.Add("Another Clan,Earth");
Clans.Add("A fourth Clan,Somewhere else");
现在,我们需要在单击按钮时输出其中的一个项目,因为知道有一个拆分器,将每个项目中的第一个值与第二个值分开。例如。Uchiha哪个是氏族的名称,第一个值是分开的,Konoha哪个是氏族的位置,第二个值,是分隔符。我们还需要创建 Random 类。让我们试试这个
Random Rand = new Random(); //Create a new Random class
int Index = Rand.Next(0, Clans.Count); //Pick up an item randomly where the minimum index is 0 and the maximum index represents the items count of Clans
string[] Categorizer = Clans[Index].Split(','); //Split the item number (Index) in Clans by ,
MessageBox.Show("Name:" + Categorizer[0] +" | Location: "+ Categorizer[1]); //Output the following
最后,它在您的表单类中看起来像这样
List<string> Clans = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
Clans.Add("Uchiha,Konoha");
Clans.Add("Picrofo Groups,Egypt");
Clans.Add("Another Clan,Earth");
Clans.Add("A fourth Clan,Somewhere else");
}
private void button1_Click(object sender, EventArgs e)
{
Random Rand = new Random();
int Index = Rand.Next(0, Clans.Count);
string[] Categorizer = Clans[Index].Split(',');
MessageBox.Show("Name:" + Categorizer[0] +" | Location: "+ Categorizer[1]);
}
谢谢,
我希望你觉得这有帮助:)