gpt4 book ai didi

c# - 在 C# 应用程序中存储多个生成的变量值

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:57 26 4
gpt4 key购买 nike

第一篇文章,我对 C# 和一般编程还很陌生。

我正在制作一个足球运动员生成器应用程序,仅用于练习目的,其中我有一个带有构造函数的类,该构造函数生成一个包含两个字符串(名字和姓氏)和大量整数的对象。 (不同的技能属性,如射门力量,传球等。

耐心等待,我会在几秒钟内发布我的代码块。

所以我希望我制作的所有这些对象都以某种方式与其所有变量一起存储。在大量谷歌搜索之后,我找到了不同种类的数组、列表和字典。

首先,这是我的类(class):

class player
{

public string firstName;
public string lastName;
public string playPos;
public int playerId = 999;
public int isKeeper;
public int level;
public int age;
public int dKeeper;
public int dTackle;
public int dMarking;
public int mPlaymaking;
public int mCrossing;
public int fShooting;
public int fShotPower;
public int aDribbling;
public int aHeading;
public int aPassing;


public void autoGenPlayer()
{
//new killing random that i got off of Stack Overflow
Random rAG = new Random(Guid.NewGuid().GetHashCode());
//PlayerId, dont mind it does not work properly yet.
playerId = playerId + 1;
//a level integer just to base the other stats around, to keep the stats from spreading too much
level = rAG.Next(2, 11);
//Generating names, calling the methods a bit lower in this class.
firstName = firstNameGenerator();
lastName = lastNameGenerator();
//giving the player an age
age = rAG.Next(16, 35);
//a one out of seven chance to become a goal keeper.
isKeeper = rAG.Next(1,8);
//skills based on level. Max value is 20
dTackle = rAG.Next(level, level * 2);
dMarking = rAG.Next(level, level * 2);
mPlaymaking = rAG.Next(level, level * 2);
mCrossing = rAG.Next(level, level * 2);
fShooting = rAG.Next(level, level * 2);
fShotPower = rAG.Next(level, level * 2);
aDribbling = rAG.Next(level, level * 2);
aHeading = rAG.Next(level, level * 2);
aPassing = rAG.Next(level, level * 2);
//Checks if the player is a keeper. 5 is chosen at random. There are obv better ways to do this, but it doesnt matter right now.
if (isKeeper == 5)
{
//This basically just makes the keeper a keeper, and a shit outfield player.
dKeeper = rAG.Next(level, level * 2);

playPos = "Goal Keeper";

dTackle = rAG.Next(1, 3);
dMarking = rAG.Next(1, 3);
mPlaymaking = rAG.Next(1, 3);
mCrossing = rAG.Next(1, 3);
fShooting = rAG.Next(1, 3);
fShotPower = rAG.Next(1, 3);
aDribbling = rAG.Next(1, 3);
aHeading = rAG.Next(1, 3);
aPassing = rAG.Next(1, 3);
}
else
{
//if not a keeper, shit keeper attributes, and random outfielder atts.
dKeeper = 1;
}
//my clever way of assigning a player position to the players.
int def = dTackle + dMarking;
int mid = mCrossing + mPlaymaking;
int fwd = fShooting + fShotPower;

if (dKeeper > 1)
{
playPos = "Goal Keeper";
}
else if (def >= fwd && def >= mid)
{
playPos = "Defender";
}
else if (mid >= fwd && mid >= def)
{
playPos = "Midfielder";
}
else if (fwd >= mid && fwd >= def)
{
playPos = "Striker";
}
else
{
//in a rare case (if ever) the logic doesnt add ut, im spawning a star player. because. im not too got at this.

playPos = "Utility Legend";
dTackle = rAG.Next(16, 21);
dMarking = rAG.Next(16, 21);
mPlaymaking = rAG.Next(16, 21);
mCrossing = rAG.Next(16, 21);
fShooting = rAG.Next(16, 21);
fShotPower = rAG.Next(16, 21);
aDribbling = rAG.Next(16, 21);
aHeading = rAG.Next(16, 21);
aPassing = rAG.Next(16, 21);
}




}

//Generates a first name
public string firstNameGenerator()
{
string returnfirstName;
string[] firstnames;
firstnames = new string[60] { "60 different strings of first names... took them out for you, becasue it looked bad in the editor." };
Random rF = new Random(Guid.NewGuid().GetHashCode());
returnfirstName = firstnames[rF.Next(0, 40)];

return returnfirstName;

}
//generates a last name
public string lastNameGenerator()
{
string returnlastName;
string[] lastnames;
lastnames = new string[60] { "60 different strings of lastnames........." };
Random rL = new Random(Guid.NewGuid().GetHashCode());
returnlastName = lastnames[rL.Next(0, 40)];
returnlastName = lastnames[rL.Next(0, 40)];
return returnlastName;

}
}

现在对于我的其他代码,您知道 - 将所有内容放在一起的部分。

namespace FormManager
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();



}

private void button1_Click(object sender, EventArgs e)
{
player p = new player();
p.autoGenPlayer();
textBox10.Text = p.firstName + " " + p.lastName;
textBox11.Text = p.age.ToString();
textBox1.Text = p.aPassing.ToString();
textBox2.Text = p.aDribbling.ToString();
textBox3.Text = p.aHeading.ToString();
textBox4.Text = p.dTackle.ToString();
textBox5.Text = p.dMarking.ToString();
textBox6.Text = p.fShooting.ToString();
textBox7.Text = p.fShotPower.ToString();
textBox8.Text = p.mPlaymaking.ToString();
textBox9.Text = p.mCrossing.ToString();
textBox12.Text = p.dKeeper.ToString();
textBox13.Text = p.playPos;


}

private void button2_Click(object sender, EventArgs e)
{
//number of objects to generate
int numberOfPlayersToGenerate = 10;
string[] savePlayers = new string[numberOfPlayersToGenerate];


//Generate many objects
for (int i = 0; i < numberOfPlayersToGenerate; i++)
{

player play = new player();
play.autoGenPlayer();



}


}


}


}

所以它基本上是我想用来存储所有生成的变量的 for 循环。autoGenPlayer() 方法生成了很多不同的 int 和 string 值,我想将它们全部存储起来,这样我就可以制作一个漂亮的表格来显示它。

我很乐意对此有任何想法,这让我很抓狂。

最佳答案

在form1中添加

List<Player> players = new List<Player>();

然后在你的 button1_Click() 中添加

players.add(p);

在 button2_Click() 中

players.add(play);

然后你可以在你的表单中添加一个 gridview 并让玩家成为数据源 - FIN

不是那么好 -

How do I bind a GridView to a custom object?如果您不知道如何设置数据源。

  • 财务

关于c# - 在 C# 应用程序中存储多个生成的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25758807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com