首页 > 解决方案 > C# - How do I call a random number generator class to main?

问题描述

So I am trying to make this wizard battle game which will heavily use Random Number Generator for plethora of things such as choosing level, name, and spells for a enemy wizard. I have generated the basic number generator but I am wondering how do I call this to Main class? Here is the code for what I have done so far. I am absolutely new to programming so I do apologize.

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

标签: c#random-seed

解决方案


Your NumberGenerator class is public however, you're going to want to make that static. The reason for this is because you don't want your NumberGenerator to be able to be instantiated. What is instantiation, I'm glad you asked.

Here's an example of instantiation.

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

Next, you need to think of your class NumberGenerator as a simple file to hold other methods. Inside of a class, you can have properties, For example, you might have a static name that is accessible from outside. However since we're doing a static class, that's not necessary. We do need a method of some sort within that class.

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

Finally, all you need to do is use it with dot syntax.

int someInt = NumberGenerator.getRandomNum();

I encourage you to go learn to build Class Objects as a next step. It's a precursor to what you're attempting to do. Build a class object that's a car with some computed properties.

Here's a great tutorial video for you to follow. https://www.youtube.com/watch?v=ZqDtPFrUonQ


推荐阅读