Я бы предложил использовать Dictionary<TKey, TValue>
:
Random rnd = new Random();
int questionNumber = rnd.Next(1, 4); //Generate a random question number
int a = rnd.Next(1, 10); //Create random numbers so the question is different each time
int b = rnd.Next(1, 15);
int c = rnd.Next(1, 15);
var questions = new Dictionary<int, string>
{
{ 1, "What is " + a + " + " + a + " ?" },
{ 2, "What is " + a + " + " + b + " ?" },
{ 3, "What is " + (a * b) + " + " + c + " ?" },
};
var question = string.Empty;
if (questions.TryGetValue(questionNumber, out question))
{
// key exists and you can read your question here
}
ОБНОВЛЕНИЕ:
Если вы хотите создать словарь в одной функции и случайные числа в другом, тогда вам нужно иметь одну целевую функцию:
static Random rnd = new Random();
static void Main(string[] args)
{
int questionQuantity = 15;
var questions = new Dictionary<int, string>();
for (int i = 0; i < questionQuantity; i++)
{
int variableCount = rnd.Next(17);
var variables = CreateVariables(variableCount);
var signs = CreateSigns(variableCount - 1);
var question = CreateQuestion(variables, signs);
questions.Add(i, question);
}
}
и другие функции:
public static List<int> CreateVariables(int variableQuantity)
{
var variables = new List<int>();
for (int i = 0; i < variableQuantity; i++)
{
variables.Add(rnd.Next(1, 15));
}
return variables;
}
public static List<char> CreateSigns(int variableQuantity)
{
var operators = new char[] {'+', '-', '/', '*' };
var randomOperators = new List<char>();
for (int i = 0; i < variableQuantity; i++)
{
randomOperators.Add(operators[ rnd.Next(0, 3) ]);
}
return randomOperators;
}
public static string CreateQuestion(List<int> variables, List<char> operations)
{
StringBuilder sb = new StringBuilder();
sb.Append("What is ");
for (int i = 0, j = 0; i < variables.Count; i++)
{
sb.Append(variables[i]);
if (i % 2 != 0)
{
sb.Append(" ");
sb.Append(operations[j]);
sb.Append(" ");
j++;
}
}
sb.Append("?");
return sb.ToString();
}