在一个中总结两个if语句

时间:2015-06-25 08:30:28

标签: java

我有很多if else if (zob.equals("")) 声明,如下面的代码所示。是否可以总结

else if (!(nameShortestDistance.equals("ABC")) 
    && !(nameShortestDistance.equals("DEF")))

if (zob.equals("")) {
    int stops_number = number_stop_in_table(macD, con);
    if (stops_number > 2) {
        Analyzer analyzer = new Analyzer(host, user, password);
        analyzer.analyze_data();
        System.out.println("Result: " + stops_number);
    }
    maintain_records(speedD, macD, con,
            shortestDistance, nameShortestDistance,
            sto_nam);
} else if (!(nameShortestDistance
        .equals("ABC"))
        && !(nameShortestDistance
                .equals("DEF"))) {
    int stops_number = number_stop_in_table(macD, con);
    if (stops_number > 2) {
        Analyzer analyzer = new Analyzer(host, user, password);
        analyzer.analyze_data();
        System.out.println("Result: "+ stops_number);
    }
    maintain_records(speedD, macD, con, shortestDistance, 
        nameShortestDistance, sto_nam);
}
一个if语句中的

语句?

代码:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    
<uses-permission android:name="android.permission.INTERNET" />

3 个答案:

答案 0 :(得分:3)

是。它被称为&#34; conditional OR operator&#34; (又名&#34;逻辑OR运算符&#34;),||

if (a || b) {
    // We get here if either operand is true
)

有关于他们的教程here on the Oracle website

答案 1 :(得分:2)

我或许会考虑完全重构,以便为测试提供方法,然后为这些方法提供有意义的名称。您可以使用以下内容:

,而不是在原始字段上使用一个大小有条件的方法
if (isSuchAndSuchPopulated() && isShortestDistance()) {
   ...
}

因此,您的方法变得更短,并且(希望)更具可读性。此外,如果暴露了这些方法,那么您可以单独对它们进行单元测试,这样可以简化生活

答案 2 :(得分:0)

这可能会有所帮助。不同类型的运营商名单。使用这些,你的选择是无止境的...花点时间学习那些......

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

复制
Simple Assignment Operator

=       Simple assignment operator
Arithmetic Operators

+       Additive operator (also used
        for String concatenation)
-       Subtraction operator
*       Multiplication operator
/       Division operator
%       Remainder operator
Unary Operators

+       Unary plus operator; indicates
        positive value (numbers are 
        positive without this, however)
-       Unary minus operator; negates
        an expression
++      Increment operator; increments
        a value by 1
--      Decrement operator; decrements
        a value by 1
!       Logical complement operator;
        inverts the value of a boolean
Equality and Relational Operators

==      Equal to
!=      Not equal to
>       Greater than
>=      Greater than or equal to
<       Less than
<=      Less than or equal to 
Conditional Operators

&&      Conditional-AND
||      Conditional-OR
?:      Ternary (shorthand for 
        if-then-else statement)
Type Comparison Operator

instanceof      Compares an object to a specified type 
Bitwise and Bit Shift Operators

~       Unary bitwise complement
<<      Signed left shift
>>      Signed right shift
>>>     Unsigned right shift
&       Bitwise AND
^       Bitwise exclusive OR
|       Bitwise inclusive OR