So, I'm programming a function that asks a random question from the dictionary until all the questions have been asked and I ran into an issue. When the list of randomly generated numbers already contains the new generated number, the program calls the Ask function, as it should. But then, when it gets to the final return line, it calls the function Ask again, instead of returning the list of numbers.
static List<int> Ask(Dictionary<string,string> Questions, List<int> random, int count)
{
bool contains = false;
var rando = new Random();
int num = Convert.ToInt32(rando.Next(0, count));
if (random.Count >= count)
{
Console.WriteLine("No more questions! Quitting");
System.Environment.Exit(1);
}
foreach (int number in random)
{
if (number == num)
{
contains = true;
}
}
if (contains == true)
{
Ask(Questions, random, count);
}
random.Add(num);
var randomEntry = Questions.ElementAt(num);
String randomKey = randomEntry.Key;
String randomValue = randomEntry.Value;
Console.WriteLine(randomKey);
if (Console.ReadLine() == randomValue)
{
Console.WriteLine("Correct!");
}
else
{
Console.WriteLine("Wrong!");
}
return random;
}
returnstatement does, in fact, exit a method. If it didn't, everything would be broken.containsistruethen, according to your code, it should recursively call theAsk()function again. So why are you surprised that it's doing exactly that? Also, why aren't you doing anything with the result of calling that function?