提问者:小点点

C#-我如何调用随机数生成器类到Main?


所以我正在尝试制作这款巫师战斗游戏,它将大量使用随机数生成器来完成诸如为敌方巫师选择等级,名字和法术之类的任务。我已经生成了基本的数字生成器,但是我想知道如何将它调用到主类?下面是我到目前为止所做工作的代码。我对编程绝对是新手,所以我真的很抱歉。

使用系统;命名空间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 ); 
    }
}

}


共2个答案

匿名用户

欢迎使用堆栈溢出。通常我们喜欢直接回答,但是,我可以看出你正在学习,所以我将以不同的方式回答这个问题,不提供直接的编码答案,以更好地帮助你理解。

您的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
}

最后,您所需要做的就是将它与点语法一起使用。

int someInt = NumberGenerator.getRandomNum();

我鼓励您下一步学习构建类对象。这是你要做的事情的前兆。构建一个class对象,它是一个具有计算属性的汽车。

这里有一个很棒的教程视频供你跟随。https://www.youtube.com/watch?v=zqdtpfruonq

匿名用户

在这里,您正在使用new关键字创建random的3个实例。最好在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类上,您有三个字符串数组,我认为它们永远不会改变(在运行时),因此您可以将statist放在它们上,这样它们就不需要对象引用来访问(您不需要创建wizards对象来访问字符串)。您也可以将它们放在RandomGenerater类中或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();       
}