加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 处理智能方式的条件

发布时间:2020-12-15 01:07:30 所属栏目:Java 来源:网络整理
导读:if (lineStyle == 5 || lineStyle == 21 || lineStyle == 82 || lineStyle == 83 || lineStyle == 3) { lineStyleString = "DOUBLE";} else if (lineStyle == 6 || lineStyle == 35 || lineStyle == 39 || lineStyle == 30) { lineStyleString = "DOTTED" ;}

if (lineStyle == 5 || lineStyle == 21 || lineStyle == 82 || lineStyle == 83 || lineStyle == 3) {
    lineStyleString = "DOUBLE";
} else if (lineStyle == 6 || lineStyle == 35 || lineStyle == 39 || lineStyle == 30) {
    lineStyleString = "DOTTED" ;
} else if (lineStyle == 26 || lineStyle == 27  || lineStyle == 28  || lineStyle == 29 || lineStyle == 1) {
    lineStyleString = "SOLID";
} else if(lineStyle == -1) {
    lineStyleString = "NONE";
}

我们如何在Java中以智能方式处理此代码?切换案例,枚举或密钥对值模式?

最佳答案
您的条件看起来更随机.

Switch在这里看起来不错

switch(lineStyle) {
    case 5:
    case 21:
    case 82:
    case 83:
    case 3: 
     lineStyleString = "DOUBLE";   
     break;
    .. // add more cases
}

或者我更喜欢创建实用方法

public static boolean contains(int expecxted,int... vals) {
        for (int i = 0; i < vals.length; i++) {
            if (expecxted == vals[i]) {
                return true;
            }
        }
        return false;
    }

你可以像使用它一样

if (contains(lineStyle,5,21,82,83,3)) {
    lineStyleString = "DOUBLE";
} else if(contains(lineStyle,6,35,39,30)){
   lineStyleString = "DOTTED";
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读