如何解决我的 StringBuilder 问题?
How can I solve my StringBuilder problem?
我在做什么: 使用 StringBuilder
替换字符串中的变量以生成包含变化的问题。
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();
}
问题: 如果字符串 q
(被修改的那个)= "Question1",StringBuilder.Replace
不会仅仅停在 sb.Replace("Question1"...)
问题2和3还是会计算的,所以随着问题的增多,效率也会越来越低。
问题:如何创建包含变量的问题,以便以有效的方式在同一问题结构中提供变化?
我建议使用 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();
}
考虑使用 switch 语句
Random rnd = new Random();
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 question;
switch (rnd.Next(1, 4)) {
case 1: {
question = "What is " + a + " + " + a + " ?";
break;
}
case 2: {
question = "What is " + a + " + " + b + " ?";
break;
}
case 3: {
question = "What is " + (a * b) + " + " + c + " ?";
break;
}
default: {
question = "Default Value";
break;
}
}
C# 8:
question = rnd.Next(1, 4) switch {
1 => "What is " + a + " + " + a + " ?",
2 => "What is " + a + " + " + b + " ?",
3 => "What is " + (a * b) + " + " + c + " ?",
_ => "Default Value"
};
我在做什么: 使用 StringBuilder
替换字符串中的变量以生成包含变化的问题。
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();
}
问题: 如果字符串 q
(被修改的那个)= "Question1",StringBuilder.Replace
不会仅仅停在 sb.Replace("Question1"...)
问题2和3还是会计算的,所以随着问题的增多,效率也会越来越低。
问题:如何创建包含变量的问题,以便以有效的方式在同一问题结构中提供变化?
我建议使用 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();
}
考虑使用 switch 语句
Random rnd = new Random();
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 question;
switch (rnd.Next(1, 4)) {
case 1: {
question = "What is " + a + " + " + a + " ?";
break;
}
case 2: {
question = "What is " + a + " + " + b + " ?";
break;
}
case 3: {
question = "What is " + (a * b) + " + " + c + " ?";
break;
}
default: {
question = "Default Value";
break;
}
}
C# 8:
question = rnd.Next(1, 4) switch {
1 => "What is " + a + " + " + a + " ?",
2 => "What is " + a + " + " + b + " ?",
3 => "What is " + (a * b) + " + " + c + " ?",
_ => "Default Value"
};