Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否有可能减少基于重复排列的if语句?_C#_Math_Optimization_Permutation_Dry - Fatal编程技术网

C# 是否有可能减少基于重复排列的if语句?

C# 是否有可能减少基于重复排列的if语句?,c#,math,optimization,permutation,dry,C#,Math,Optimization,Permutation,Dry,您好,我正在做这个数学游戏,在我的最后一个场景中,我做了很多重复的代码,但我不确定是否有办法简化它,我在下面链接了,所以也许一些经验丰富的程序员可能会有一些更优雅的解决方案! 例如,我试图生成(a[]b)²[]c[]d之类的每个排列,其中括号将替换为+、-、*、或/。我一直在做的只是创建随机百分比if语句来选择一个特定的版本,比如“(a+b)²/c-d”是否有一种比我所做的更少的“暴力”和可读性的方法 if(UnityEngine.Random.Range(0,101)>50){

您好,我正在做这个数学游戏,在我的最后一个场景中,我做了很多重复的代码,但我不确定是否有办法简化它,我在下面链接了,所以也许一些经验丰富的程序员可能会有一些更优雅的解决方案! 例如,我试图生成(a[]b)²[]c[]d之类的每个排列,其中括号将替换为+、-、*、或/。我一直在做的只是创建随机百分比if语句来选择一个特定的版本,比如“(a+b)²/c-d”是否有一种比我所做的更少的“暴力”和可读性的方法

if(UnityEngine.Random.Range(0,101)>50){
        // 50% of being (a+b)²(c)+d
    if(UnityEngine.Random.Range(0,101)>50){
        ans = ((int) Mathf.Pow((float) a+ (float) b, 2))*c+d;
        input.text = "("+a+"+"+b+")"+"²"+"("+c+")"+"+"+d+"=";
        Debug.Log("Problem ID: 78");
        // 50% of being (a+b)²(c)-d
    } else {
        ans = ((int) Mathf.Pow((float) a+ (float) b, 2))*c-d;
        input.text = "("+a+"+"+b+")"+"²"+"("+c+")"+"-"+d+"=";
        Debug.Log("Problem ID: 79");
    }
    // 50% of being (a-b)²(c)[]d
} else {
    // 50% of being (a-b)²(c)+d
    if(UnityEngine.Random.Range(0,101)>50){
        ans = ((int) Mathf.Pow((float) a- (float) b, 2))*c+d;
        input.text = "("+a+"-"+b+")"+"²"+"("+c+")"+"+"+d+"=";
        Debug.Log("Problem ID: 80");
        // 50% of being (a-b)²(c)-d
    } else {
        ans = ((int) Mathf.Pow((float) a- (float) b, 2))*c-d;
        input.text = "("+a+"-"+b+")"+"²"+"("+c+")"+"-"+d+"=";
        Debug.Log("Problem ID: 81");
    }
(有关更多上下文,请参见下面的粘贴栏)

将计算分为三部分-
a±b
square*c
±d
。分别计算它们,然后将它们相乘得到最终结果。对于文本,可以使用字符串插值

float ans;
string operator1;
string operator2;
if (UnityEngine.Random.Range(0,101)>50) {
    ans = (float) a + (float) b;
    operator1 = "+";
} else {
    ans = (float) a - (float) b;
    operator1 = "-";
}
ans = (int)(ans * ans) * c;
if (UnityEngine.Random.Range(0,101)>50) {
    ans += d;
    operator2 = "+";
} else {
    ans -= d;
    operator2 = "-";
}
input.text = $"(a{operator1}b)²(c){operator2}d";
还要注意,
UnityEngine.Random.Range(0101)>50
并不是50%的概率。您可能是指
UnityEngine.Random.Range(1101)>50
,但我会使用
UnityEngine.Random.Range(0,2)==0

通过生成2个随机位,并将这些位编码的2位数字与78相加,可以将问题ID展平:

float ans;
string operator1;
string operator2;
int random1 = UnityEngine.Random.Range(0,2);
int random2 = UnityEngine.Random.Range(0,2);
if (random1 == 0) {
    ans = (float) a + (float) b;
    operator1 = "+";
} else {
    ans = (float) a - (float) b;
    operator1 = "-";
}
ans = (int)(ans * ans) * c;
if (random2 == 0) {
    ans += d;
    operator2 = "+";
} else {
    ans -= d;
    operator2 = "-";
}
int problemID = 78 + random1 + random2 * 2;
Debug.Log($"Problem ID: {problemID}");
input.text = $"(a{operator1}b)²(c){operator2}d";

不过,这个技巧在我看来不是特别可读。

我很高兴您希望让代码更可读。基本思想是(a)定义,(b)选择和(c)应用运算符

  • 步骤1:定义
    运算符
    s。每个
    操作符
    组合了一个数学运算(例如
    添加
    将是
    (a,b)=>a+b
    )和一个符号(例如
    添加
    将是
    “+”

  • 步骤3:应用它们:

    var (op1, op2, problemId) = RandomlyChooseProblem();
    
    ans = op2.Calculate((int)Math.Pow(op1.Calculate(a, b), 2) * c, d);
    input.text = $"(a{op1.Symbol}b)²*c{op2.Symbol}d");
    Debug.Log($"Problem ID: {problemId}");
    

添加一个新的操作符(例如,
Multiply
)或一个新的问题变量(例如,
(Add,Multiply,82)
)现在只是一行代码。

因此,如果我想简化它,我应该使用一个类似(a[]b)的模型[]c[]d,然后为这段代码生成一个[+,-,*,/]b,并为每个变量交互执行该操作。并可能将其抽象为。一个switch-case方法,这样当我做像A[]b[]c[]d或A[]b[](c[]d)@BriceBrown这样的事情时,我可以重复它。如果你有那么多操作符,你可能应该试试海因茨的答案。
var rnd = new Random();

private (Operator op1, Operator op2, int problemId) RandomlyChooseProblem()
{
    switch (rnd.Next(4))
    {
        case 0: return (Add, Add, 78);
        case 1: return (Add, Subtract, 79);
        case 2: return (Subtract, Add, 80);
        case 3: return (Subtract, Subtract, 81);
        default: throw new InvalidOperationException("This should not happen.");
    }
}
var (op1, op2, problemId) = RandomlyChooseProblem();

ans = op2.Calculate((int)Math.Pow(op1.Calculate(a, b), 2) * c, d);
input.text = $"(a{op1.Symbol}b)²*c{op2.Symbol}d");
Debug.Log($"Problem ID: {problemId}");