What I am doing: Replacing variables within a string using StringBuilder to generate questions that contain variation.
string question;
void CreateNewQuestion()
{
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);
string q = questionNumber.ToString();
StringBuilder sbq = new StringBuilder("Question" +q);//StringBuilder is now called to replace the randomly called for question with its new variables
sbq.Replace("Question1", $"What is {a} + {a} ?");
sbq.Replace("Question2", $"What is {a} + {b} ?");
sbq.Replace("Question3", $"What is {a*c} + {a} ?"");
question = sbq.ToString();
}
The problem: If string q (the one being modified) = "Question1", StringBuilder.Replace won't just stop at sb.Replace("Question1"...) it will still calculate for Question 2 and 3. Thus as the number of questions increases, so does the inefficiency of this.
The question: How can I create questions that contain variables so as to provide variation within the same question structure in an efficient manner?
$"What is {a} + {b}?". It helps a lot, especially in your case in which string conjunction + are mixed with mathematical +."What is {a} + {b}?"in an array or dictionary, and look them up by question number. Make them formatting strings so that you can still fill in the holes. See learn.microsoft.com/en-us/dotnet/csharp/language-reference/…