I am creating a console application for a small quiz. I made a list with three questions. How can I let the program randomly select a question and print it in the console?
I tried several different codes, but apparently it somehow didn't work. This is the last code I tried that I received from another user from this site, but I get errors:
"The name 'string' does not exists in current context"
"Since 'Quiz.Questions.main ()' returns void, the return keyword should not be accompanied by an object expression
Here is the last piece of code I tried:
class Questions { public static void main() { var questions = new List<string>{ "question1", "question2", "question3"}; int index = Random.Next(strings.Count); questions.RemoveAt(index); return questions; }
}
Thank you all for your answers. I fixed my problem by creating an array instead of a list. This is my code now:
class Questions { public static void main() { string[] questions = new string[3]; questions[0] = "question1"; questions[1] = "question2"; questions[2] = "question3"; Random rnd = new Random(); Console.WriteLine(questions[rnd.Next(0,2)]); } }
source share