[英]c# Can I do this for “if”? or any other way to doing something like this?
if ((((a > 0) && ((b > 0) || (c> 0) || (d> 0) || (e> 0))) ||
((a1 > 0) && ((b> 0) || (c > 0) || (d > 0) || (e> 0))) ||
((a2 > 0) && ((b> 0) || (c> 0) || (d> 0) || (e> 0))) ||
((a3> 0) && ((b> 0) || c> 0) || (d> 0) || (e> 0)))) &&
((75.5 > d1 && d1 > 74.5) || (75.5 > d2 && d2 > 74.5)))
{
//do something
}
just want to make the condition of the IF as the following: 只想使IF的条件如下:
(a > 0 and( b > 0 or c > 0 or d > 0 or e > 0)) and ((d1< 75.5 and d1>74.5 ) or (d2<75.5 and d2>74.5)) or (a1 > 0 and( b > 0 or c > 0 or d > 0 or e > 0)) and ((d1< 75.5 and d1>74.5 ) or (d2<75.5 and d2>74.5)) or (a2 > 0 and( b > 0 or c > 0 or d > 0 or e > 0)) and ((d1< 75.5 and d1>74.5 ) or (d2<75.5 and d2>74.5)) or (a3 > 0 and( b > 0 or c > 0 or d > 0 or e > 0)) and ((d1< 75.5 and d1>74.5 ) or (d2<75.5 and d2>74.5)) (a> 0和(b> 0或c> 0或d> 0或e> 0))和((d1 <75.5和d1> 74.5)或(d2 <75.5和d2> 74.5))或(a1> 0和(b> 0或c> 0或d> 0或e> 0))和((d1 <75.5和d1> 74.5)或(d2 <75.5和d2> 74.5))或(a2> 0和(b> 0或c> 0或d> 0或e> 0))和((d1 <75.5和d1> 74.5)或(d2 <75.5和d2> 74.5))或(a3> 0和(b> 0或c> 0或d> 0或e> 0))和((d1 <75.5和d1> 74.5)或(d2 <75.5和d2> 74.5))
for the same conditions for a1 to a3 对于a1到a3的相同条件
cos I have created a for loop before these code to do some checking , and those a,a1,a2,a3,b,c,d,e are the ints which have added 1 under some special condition in the for loop, and i just want to do some further grouping in this if() and just wonder if this multi "Ands" and "Ors" will work or not 我在这些代码之前创建了一个for循环来进行一些检查,那些a,a1,a2,a3,b,c,d,e是在for循环中在某些特殊条件下加1的整数,而我只是想在这个if()中做一些进一步的分组,并且只是想知道这个多“Ands”和“Ors”是否会起作用
I would break this out into more booleans to simplify it 我会把它分解成更多的布尔值以简化它
bool biggerZero = (b > 0) || (c> 0) || (d> 0) || (e> 0));
bool anAzero = a > 0 || a1 > 0 || a2 > 0 || a3 > 0;
bool d1range = 75.5 > d1 && d1 > 74.5;
bool d2range = 75.5 > d2 && d2 > 74.5;
bool solution = anAzero && biggerZero && (d1range || d2range);
Generally it is much clearer to place the common subexpressions in separate variables when they are repeated in a complex expression. 通常,当在复杂表达式中重复使用公共子表达式时,将它们放在单独的变量中会更加清晰。 That should make no difference to the compiler in terms of optimization.
在优化方面,这对编译器没有任何影响。
Your parenthesis are not balanced so I'm not 100% sure what your code is expressing, but for example you could declare 你的括号不平衡,所以我不能100%确定你的代码表达的是什么,但是你可以声明
bool aThroughEGreaterEqualZero = (b > 0) || (c> 0) || (d> 0) || (e> 0);
and use that in place of all occurrences of that evaluation in your if
. 并使用它代替
if
中所有出现的评估。
For the sake of the next person to maintain the code, I would break down the expression in the if
statement into a number of meaningfully-named variables, and then use those variables in the actual if
. 为了让下一个人维护代码,我会将
if
语句中的表达式分解为一些有意义的命名变量,然后在实际的if
使用这些变量。
You can make a helper method for checking the repetitive code like: 您可以创建一个帮助方法来检查重复代码,如:
Func<bool> conditionExpression => (int a, int b, int c, int d, int e) =>
{
return a > 0 && (b | c |d | e) > 0;
}
bool dWithinRange = (d1 < 75.5 && d1 > 74.5 ) || (d2 < 75.5 and d2 > 74.5);
if (conditionExpression(a, b, c, d, e) &&
conditionExpression(a1, b1, c1, d1, e1) &&
conditionExpression(a2, b2, c2, d2, e2) &&
conditionExpression(a3, b3, c3, d3, e3) &&
dWithinRange
{
// finally
}
I queess however that the most helpfull modification would be for giving your variables understandable names. 然而,我觉得最有帮助的修改是给你的变量可理解的名字。 It's unlikely that a reader of your code will understand what d1 is and why it has to be smaller than ...
您的代码读者不太可能理解d1是什么以及为什么它必须小于...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.