如何解决我的StringBuilder问题?

3

我所做的: 使用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个问题。 因此,随着问题数量的增加,效率也会变得低下。

问题: 如何以高效的方式创建包含变量的问题,以便在相同的问题结构内提供变化?


3
为了提高可读性,我建议您使用以下形式:$" {a} + {b} 是多少?". 这样做可以很大程度上帮助您,特别是在字符串连接符“+”与数学符号“+”混用的情况下。 - Alvin Sartor
2
你的代码没有太多意义。你将"Question" + q放入一个字符串中,但随后立即用与问题编号无关的其他内容替换了它。 - Robert Harvey
1
嗨,已经更新完成了 - 希望能有所帮助。 - kizzer
2
然后我会将字符串"What is {a} + {b}?"放入数组或字典中,并按问题编号查找它们。将它们设置为格式化字符串,以便仍然可以填充空缺。请参见https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated - Robert Harvey
显示剩余8条评论
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();
}

3
使用一个数组,通过索引访问它。 - Irdis
@irdis:数组大小固定。 - Robert Harvey
@Irdis 如果 OP 想要将键作为 string 值,例如 Question1,则可以使用 Dictionary<TKey, TValue>。 - StepUp
1
那真是一件艺术品,谢谢!多么不可思议的答案。 - kizzer
1
@kizzer 非常感谢!这对我来说意义重大!祝你有美好的一天! :) - StepUp
显示剩余2条评论

3
考虑使用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"
        };

PeterT你好,我能否将整数放在一个函数中,在每个case开始时调用该函数?这样每次生成问题时都会选择新的变量?您认为这种方法有效吗?谢谢。 - kizzer
@kizzer 你的意思是将整数传递到函数中,在此switch语句中,然后返回结果吗? - PeterT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接