简体   繁体   English

有没有办法缩短重复数学

[英]Is there a way to shorten repetitive math

I have a math equation that is repeated for multiple variable but each time only the output and 1 variable is changed我有一个对多个变量重复的数学方程,但每次只更改输出和 1 个变量

R1S2NCtxt.Text = ((((ATT * R1S2) - EDEF) * 0.8M) + ((ATT * Pierce) - EDEF * ERES)).ToString();
R2S2NCtxt.Text = ((((ATT * R2S2) - EDEF) * 0.8M) + ((ATT * Pierce) - EDEF * ERES)).ToString();
R3S2NCtxt.Text = ((((ATT * R3S2) - EDEF) * 0.8M) + ((ATT * Pierce) - (EDEF * ERES))).ToString();

This is repeated multiple times is there a way to shorten this or repeat formula with different variables thx这是重复多次有没有办法缩短这个或用不同的变量重复公式thx

You could use a function to get rid of copy pasting code.您可以使用函数来摆脱复制粘贴代码。

Example:例子:

private string Calculate(float number) {
    return  ((((ATT * number) - EDEF) * 0.8M) + ((ATT * Pierce) - EDEF * ERES)).ToString();
}

Now you can call that function with:现在您可以使用以下命令调用该函数:

R1S2NCtxt.Text = Calculate(R1S2);
R2S2NCtxt.Text = Calculate(R2S2);
R3S2NCtxt.Text = Calculate(R3S2);
decimal Equation(decimal value)
    => (((ATT * value) - EDEF) * 0.8M) + ((ATT * Pierce) - EDEF * ERES);

Then:然后:

R1S2NCtxt.Text = Equation(R1S2).ToString();

Assuming R1S2 is decimal ;假设R1S2decimal if not then just amend the type of value accordingly.如果不是,那么只需相应地修改value的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM