c# - 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 );
}
}
}
解决方案
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
推荐阅读
- node.js - nuxtjs中的不匹配vue包错误
- magnolia - 执行操作时发生错误
- c# - 如何在 Task.WhenAll 中分配变量
- docker - 如何基于每个 pod 从 Kubernetes 向 GELF UDP 端点发送日志
- jquery - 简单回调函数未按预期工作
- .htaccess - 301重定向到错误的url
- android - 使用 Clang 作为编译器构建 Android 项目时从静态库中隐藏符号
- php - 如果某个数组中的值等于另一个数组中的值,则更改该数组中的值
- java - OracleDriver 不接受 jdbcUrl jdbc:oracle:thin@localhost:1521/orcl
- c++ - 一个类可以只继承成员的声明而没有定义吗?