java - Java If Else 语句的简写形式

标签 java if-statement ternary-operator short

我有一个检查空值的方法。有没有办法减少方法中的行数?目前,代码看起来很“脏”:

private int similarityCount (String one, String two) {

    if (one == null && two == null) {
        return 1;
    } else if (one == null && two != null) {
        return 2;
    } else if (one != null && two == null) {
        return 3;
    } else {
        if(isMatch(one, two))
             return 4;
        return 5;
    }

}

最佳答案

private int similarityCount (String one, String two) {

    if (one == null && two == null) {
        return 1;
    } 

    if (one == null) {
        return 2;
    } 

    if (two == null) {
        return 3;
    } 

    if (isMatch(one, two)) {
        return 4;
    }
    return 5;
}

关于java - Java If Else 语句的简写形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41999782/

相关文章:

java - 带有 Comparator<?> 参数的 TreeSet 构造函数

java - 代码在应该打印最大值和最小值时打印出数组的最大下标

if-statement - if/else vs 三元运算符

algorithm - 嵌套 if 与 and 条件

java - 将 "condition ? expr1 : expr2"的链转换为 "if"的链

javascript - JSON 到 HTML : Make Templates depending on JSON field value (ES6 only)

java - 在 Intellij 的插件部分中找不到 Drool 插件

java - JAXB 继承动态命名标签

Java:优化代码

c++ - for循环:指针变量作为if语句中的条件的必要性