-3

我需要改进这段代码,很重,我没有找到另一种方法来创建

所有条件都需要验证,如果条件为真并不重要,必须继续寻找其他条件。

对不起我的英语谷歌翻译

if (skill.getId() == 233 && getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 16 || getActiveClassId() == 30 || getActiveClassId() == 17 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 105 || getActiveClassId() == 98 || getActiveClassId() == 112 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 116 || getActiveClassId() == 116 || getActiveClassId() == 28 || getActiveClassId() == 41 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 111 || getActiveClassId() == 96) {
    removeSkillById(233); // Light Armor
} else {
    // Nothing...
}
if (skill.getId() == 227 && getActiveClassId() == 36 || getActiveClassId() == 9 || getActiveClassId() == 37 || getActiveClassId() == 23 || getActiveClassId() == 24 || getActiveClassId() == 8 || getActiveClassId() == 48 || getActiveClassId() == 108 || getActiveClassId() == 92 || getActiveClassId() == 109 || getActiveClassId() == 101 || getActiveClassId() == 102 || getActiveClassId() == 93 || getActiveClassId() == 114 || getActiveClassId() == 16 || getActiveClassId() == 30 || getActiveClassId() == 17 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 105 || getActiveClassId() == 98 || getActiveClassId() == 112 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116 || getActiveClassId() == 28 || getActiveClassId() == 41 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 111 || getActiveClassId() == 96) {
    removeSkillById(227); // Light Armor
} else {
    // Nothing...
}
if (skill.getId() == 236 && getActiveClassId() == 36 || getActiveClassId() == 9 || getActiveClassId() == 37 || getActiveClassId() == 23 || getActiveClassId() == 24 || getActiveClassId() == 8 || getActiveClassId() == 48 || getActiveClassId() == 108 || getActiveClassId() == 92 || getActiveClassId() == 109 || getActiveClassId() == 101 || getActiveClassId() == 102 || getActiveClassId() == 93 || getActiveClassId() == 114 || getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116 || getActiveClassId() == 28 || getActiveClassId() == 41 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 111 || getActiveClassId() == 96) {
    removeSkillById(236); // Light Armor
} else {
    // Nothing...
}
if (skill.getId() == 252 && getActiveClassId() == 36 || getActiveClassId() == 9 || getActiveClassId() == 37 || getActiveClassId() == 23 || getActiveClassId() == 24 || getActiveClassId() == 8 || getActiveClassId() == 48 || getActiveClassId() == 108 || getActiveClassId() == 92 || getActiveClassId() == 109 || getActiveClassId() == 101 || getActiveClassId() == 102 || getActiveClassId() == 93 || getActiveClassId() == 114 || getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 16 || getActiveClassId() == 30 || getActiveClassId() == 17 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 105 || getActiveClassId() == 98 || getActiveClassId() == 112 || getActiveClassId() == 28 || getActiveClassId() == 41 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 111 || getActiveClassId() == 96) {
    removeSkillById(252); // Light Armor
} else {
    // Nothing...
}
if (skill.getId() == 258 && getActiveClassId() == 36 || getActiveClassId() == 9 || getActiveClassId() == 37 || getActiveClassId() == 23 || getActiveClassId() == 24 || getActiveClassId() == 8 || getActiveClassId() == 48 || getActiveClassId() == 108 || getActiveClassId() == 92 || getActiveClassId() == 109 || getActiveClassId() == 101 || getActiveClassId() == 102 || getActiveClassId() == 93 || getActiveClassId() == 114 || getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 16 || getActiveClassId() == 30 || getActiveClassId() == 17 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 105 || getActiveClassId() == 98 || getActiveClassId() == 112 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116) {
    removeSkillById(258); // Light Armor
} else {
    // Nothing...
}
if (skill.getId() == 231 && getActiveClassId() == 6 || getActiveClassId() == 5 || getActiveClassId() == 33 || getActiveClassId() == 20 || getActiveClassId() == 91 || getActiveClassId() == 90 || getActiveClassId() == 106 || getActiveClassId() == 99 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116 || getActiveClassId() == 17 || getActiveClassId() == 98) {
    removeSkillById(231); // Heavy Armor
} else {
    // Nothing...
}
if (skill.getId() == 232 && getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116 || getActiveClassId() == 17 || getActiveClassId() == 98) {
    removeSkillById(232); // Heavy Armor
} else {
    // Nothing...
}
if (skill.getId() == 253 && getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 6 || getActiveClassId() == 5 || getActiveClassId() == 33 || getActiveClassId() == 20 || getActiveClassId() == 91 || getActiveClassId() == 90 || getActiveClassId() == 106 || getActiveClassId() == 99 || getActiveClassId() == 17 || getActiveClassId() == 98) {
    removeSkillById(253); // Heavy Armor
} else {
    // Nothing...
}
if (skill.getId() == 259 && getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 6 || getActiveClassId() == 5 || getActiveClassId() == 33 || getActiveClassId() == 20 || getActiveClassId() == 91 || getActiveClassId() == 90 || getActiveClassId() == 106 || getActiveClassId() == 99 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116) {
    removeSkillById(259); // Heavy Armor
} else {
    // Nothing...
}
if (skill.getId() == 234 && getActiveClassId() == 16 || getActiveClassId() == 17 || getActiveClassId() == 30 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 98 || getActiveClassId() == 105 || getActiveClassId() == 112 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116) {
    removeSkillById(234); // Robe Mastery
} else {
    // Nothing...
}
if (skill.getId() == 235 && getActiveClassId() == 28 || getActiveClassId() == 13 || getActiveClassId() == 41 || getActiveClassId() == 12 || getActiveClassId() == 40 || getActiveClassId() == 27 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 95 || getActiveClassId() == 111 || getActiveClassId() == 94 || getActiveClassId() == 110 || getActiveClassId() == 103 || getActiveClassId() == 96 || getActiveClassId() == 51 || getActiveClassId() == 52 || getActiveClassId() == 115 || getActiveClassId() == 116) {
    removeSkillById(235); // Robe Mastery
} else {
    // Nothing...
}
if (skill.getId() == 251 && getActiveClassId() == 28 || getActiveClassId() == 13 || getActiveClassId() == 41 || getActiveClassId() == 12 || getActiveClassId() == 40 || getActiveClassId() == 27 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 95 || getActiveClassId() == 111 || getActiveClassId() == 94 || getActiveClassId() == 110 || getActiveClassId() == 103 || getActiveClassId() == 96 || getActiveClassId() == 16 || getActiveClassId() == 17 || getActiveClassId() == 30 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 98 || getActiveClassId() == 105 || getActiveClassId() == 112) {
    removeSkillById(251); // Robe Mastery
} else {
    // Nothing...
}

有什么帮助我吗?谢谢!!

4

2 回答 2

2

你可以做几件事来优化它。

  1. 删除以下代码块,它们是不必要的:

    else 
    {
        // Nothing..
    }
    
  2. 接下来,将函数的返回值保存到局部变量中,以节省多次调用同一个函数的开销。

    int activeClassId = getActiveClassId(), skillId = skill.getId();
    
  3. 使用 aList存储可能的值,并使用List.contains(). 例如,您现在可以转换它:

    if(skill.getId() == 252 && getActiveClassId() == 36 || getActiveClassId() == 9 || getActiveClassId() == 37 || getActiveClassId() == 23 || getActiveClassId() == 24 || getActiveClassId() == 8 || getActiveClassId() == 48 || getActiveClassId() == 108 || getActiveClassId() == 92 || getActiveClassId() == 109 || getActiveClassId() == 101 || getActiveClassId() == 102 || getActiveClassId() == 93 || getActiveClassId() == 114 || getActiveClassId() == 55 || getActiveClassId() == 46 || getActiveClassId() == 2 || getActiveClassId() == 3 || getActiveClassId() == 57 || getActiveClassId() == 117 || getActiveClassId() == 113 || getActiveClassId() == 88 || getActiveClassId() == 89 || getActiveClassId() == 118 || getActiveClassId() == 16 || getActiveClassId() == 30 || getActiveClassId() == 17 || getActiveClassId() == 43 || getActiveClassId() == 97 || getActiveClassId() == 105 || getActiveClassId() == 98 || getActiveClassId() == 112 || getActiveClassId() == 28 || getActiveClassId() == 41 || getActiveClassId() == 14 || getActiveClassId() == 104 || getActiveClassId() == 111 || getActiveClassId() == 96)
    

    进入这个:

    List<Integer> validClassIds = Arrays.asList( 36, 9, 37, 23, 24, 8, 48, 108, 92, 109, 101, 102, 93, 114, 55, 46, 2, 3, 57, 117, 113, 88, 89, 118, 16, 30, 17, 43, 97, 105, 98, 112, 28, 41, 14, 104, 111, 96);
    
    if( skillId == 252 && validClassIds.contains( activeClassId))
    

如果您不想使用 进行O(n)搜索List.contains(),可以使用 HashSet 而不是 List。更多信息在这里

于 2013-02-16T04:29:55.233 回答
1

我从算法的角度有建议。

使用霍夫曼编码通过调整 if-else 条件序列来最小化比较时间。如果您有数据的可能性统计。

于 2013-02-16T04:39:54.263 回答