有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将两条if语句汇总为一条

我有许多ifif else语句,如下代码所示。有没有可能对这个问题进行总结

 if (zob.equals("")) 

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

一个if语句条件中的语句

代码:

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);
}

共 (3) 个答案

  1. # 1 楼答案

    也许我会考虑重构,这样你就有了测试的方法,然后给那些方法有意义的名字。您不必在原语字段上使用一个带有条件的大型方法,而可以使用如下内容:

    if (isSuchAndSuchPopulated() && isShortestDistance()) {
       ...
    }
    

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

  2. # 2 楼答案

    这可能会有帮助。不同类型操作员的列表。使用这些,你的选择是无穷无尽的。。。花点时间学习这些

    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
    
  3. # 3 楼答案

    对。它被称为“conditional OR operator”(也称为“逻辑或运算符”),||

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

    有一个关于它们的教程