I'm pretty new to coding though I have a fairly okay-ish grasp on some of the basics. One thing I've been really struggling with, however, is using "Random". I'm currently trying to create something that will allow me to generate random elements from a class in my program. How could I tweak the code below to allow for this? I tried creating a class with a few elements that could potentially be generated but this program only allows me to call strings or integers currently.
namespace Random_practice
{
class Program
{
static void Main(string[] args)
{
var e = new Random();
var myList = new List<string> {"tom","paul","jim" };
int count = myList.Count;
int indexValue = e.Next(count);
Console.WriteLine(myList[indexValue]);
Console.ReadLine();
}
}
}
I basically want to be able to place class elements where the names tom, paul, and jim are and have the program randomly pull one up when run. I realize this is probably a pretty nooby question but I'm trying to learn! lol
This is an example of a class I'd like to pull from:
namespace Random_practice
{
public class Encounters
{
public static void Encounter1()
{
Console.WriteLine("You encounter Tom!");
Console.ReadLine();
}
public static void Encounter2()
{
Console.WriteLine("You encounter Paul!");
Console.ReadLine();
}
public static void Encounter3()
{
Console.WriteLine("You encounter Jim!");
Console.ReadLine();
}
}
}
Basically I want to be able to use something like "myEncounters.Encounter1" in my list of elements for the program to choose from in place of just typing out names in string form.
Aucun commentaire:
Enregistrer un commentaire