C#-如何将随机数生成器类调用到main



所以我正在尝试制作这个巫师战斗游戏,它将大量使用随机数生成器来为敌方巫师选择等级、名称和咒语等。我已经生成了基本的数字生成器,但我想知道如何将其称为Main类?以下是我迄今为止所做工作的代码。我对编程完全陌生,所以我很抱歉。

using System;
namespace Battle_Wizard
{
class Program
{
static void Main(string[] args)
{
string Player;
Console.WriteLine("Hello Wizard! What is your name: (Insert your name) ");
Player = Console.ReadLine();
Console.WriteLine("So your name is "+ Player + "? " + "What a stupid name. nPRESS ANY BUTTON FOR A BATTLE!");
Console.ReadKey();

}
public class Wizards 
{
string [] names = {"Ifeus","Avutaz","Alvapan","Inawyn","Agrukey","Unageor","Anvigron","Ubus","Enoviar","Unitor"};
string [] spells = {"A Alakablam ","Y0ur m0m ","A Karate Chop ","Abra-kadabra ","A 12 Gauge Shotgun ","Telekinesis "};
string [] deathmessages = {" set their pants on fire by ", " shot in the face with a ", " perished painfully with  "};
}
public class NumberGenerator 
{
Random rnd = new Random();
int EnemyRoll = new Random().Next( 1, 10 );
int PlayerRoll = new Random().Next( 1, 10 ); 
}
}
}

这里使用new关键字创建了3个Random实例。最好在NumberGenerator类中使用一个实例。

public class NumberGenerator 
{
Random rnd = new Random();
int EnemyRoll = new Random().Next( 1, 10 );
int PlayerRoll = new Random().Next( 1, 10 ); 
}

我会这样重写这个类:

public class NumberGenerator
{
Random rnd;
public NumberGenerator() 
{
rnd = new Random();
}
// use seperate methods for each thing you want to generate
int generateEnemyRoll()
{
return rnd.Next(1, 10);
}
int generatePlayerRoll()
{
return rnd.Next(1, 10);
}
string generateDeathMessage()
{
return Wizards.deathmessages[rnd.Next(0, Wizards.deathmessages.Length)];
}
// etc for all the other things you need to generate
}

此外,在Wizards类上,有三个字符串数组,我认为它们永远不会改变(在运行时(,所以可以将static放在它们上面,这样它们就不需要对象引用来访问(不需要创建Wizard对象来访问字符串(。您也可以将这些放在RandomGenerator类或generateDeathMessage()方法中。

就像这样…

public class Wizard
{
static string [] names = {"Ifeus","Avutaz","Alvapan","Inawyn","Agrukey","Unageor","Anvigron","Ubus","Enoviar","Unitor"};
static string [] spells = {"A Alakablam ","Y0ur m0m ","A Karate Chop ","Abra-kadabra ","A 12 Gauge Shotgun ","Telekinesis "};
static string [] deathmessages = {" set their pants on fire by ", " shot in the face with a ", " perished painfully with  "};
}

如何从主类使用?Main是一个方法,而不是一个类,但从Main方法可以做到这一点。。。

static void Main(string[] args)
{
string Player;
Console.WriteLine("Hello Wizard! What is your name: (Insert your name) ");
Player = Console.ReadLine();
Console.WriteLine("So your name is "+ Player + "? " + "What a stupid name. nPRESS ANY BUTTON FOR A BATTLE!");
NumberGenerator generator = new NumberGenerator();
int enemyRoll = generator.generateEnemyRoll();
int playerRoll = generator.generatePlayerRoll();
string deathMessage = generator.generateDeathMessage();
// etc
Console.ReadKey();       
}

您的NumberGenerator类是公共的,但是您需要将其设置为静态的。原因是您不希望NumberGenerator能够被实例化。什么是实例化,我很高兴你问。

这里有一个实例化的例子

public class Car {
//CAR properties sometimes referred to as "Member Variables."
private string _color;
private int _wheels;
//Initialization method. This is what tells the compiler what goes where and it's typing. 
public Car (string color, int wheels) {
_color = color;
_wheels = wheels;
}
}
public SomeClass {
//Instantiation of that car we created above. 
Car someCar = new Car ("Blue", 4);

//Using the properties of that Car object.
string someColor = someCar.wheels;
int someWheelNum = someCar.wheels;
}

接下来,您需要将类NumberGenerator视为一个简单的文件来容纳其他方法。在类内部,您可以有属性。例如,您可能有一个可以从外部访问的静态名称。然而,由于我们是在做一个静态类,所以没有必要这样做。我们确实需要该类中的某种方法。

public int getRandomNum() {
//Do something here.
return someInt
}

最后,您所需要做的就是使用dot语法。

int someInt = NumberGenerator.getRandomNum();

我鼓励您下一步去学习构建类对象。这是你尝试做什么的前兆。构建一个类对象,它是一辆具有一些计算属性的汽车。

这里有一个很棒的教程视频供您观看。https://www.youtube.com/watch?v=ZqDtPFrUonQ

最新更新