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

C# - How do I call a random number generator class to main?

所以我正在尝试制作这款巫师对战游戏,它将大量使用随机数生成器来处理过多的事情,例如为敌方巫师选择等级、名称和法术。我已经生成了基本数字生成器,但我想知道如何将其调用到 Main class?这是我到目前为止所做的代码。我对编程完全陌生,所以我深表歉意。

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 ); 
        }
    }
}

您的 NumberGenerator class 是 public 但是,您需要将其设为静态。这样做的原因是因为您不希望您的 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;
}

接下来,您需要将 class NumberGenerator 视为一个包含其他方法的简单文件。在 class 内部,您可以拥有属性,例如,您可能拥有可从外部访问的静态名称。但是,由于我们正在执行静态 class,因此没有必要。我们确实需要 class.

中的某种方法
public int getRandomNum() {
    //Do something here.
    return someInt
}

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

int someInt = NumberGenerator.getRandomNum();

我鼓励您下一步去学习构建 Class 对象。它是您尝试做的事情的前奏。构建一个 class 对象,该对象是具有某些计算属性的汽车。

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

在这里,您将使用 new 关键字创建 Random 的 3 个实例。最好在 NumberGenerator class.

中使用单个实例
public class NumberGenerator 
{
    Random rnd = new Random();
    int EnemyRoll = new Random().Next( 1, 10 );
    int PlayerRoll = new Random().Next( 1, 10 ); 
}

我会这样重写 class:

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 class 上,你有三个字符串数组,我认为它们永远不会改变(在运行时),所以你可以将 static 放在它们上面所以他们不需要对象引用来访问(您不需要创建 Wizard 对象来访问字符串)。您也可以将它们放在 RandomGenerator class 或 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  "};
}

如何从主class使用? Main 不是 class 的方法,但是从 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();       
}