如何减少此代码中的else语句?

时间:2019-10-27 13:34:37

标签: java oop

这里的代码基本上用于根据骰子的滚动来确定字符串。在这方面有很多陈述,我确实可以使用一些帮助来减少这种情况。 我已经在互联网上查看了一些方法,但它们并不适合。

public String generatePassageSection(){
        int roll = roll();
        if(roll<=2 && roll>=1) 
        {
            return "passage goes straight for 10 feet.";
        }

        else if(roll<=5 && roll>=3) 
        {
            return "passage ends in door to a chamber.";
        }

        else if(roll<=7 && roll>=6) 
        {
            return "door to right (main passage continues straight for 10 ft)";
        }

        else if(roll<=9 && roll>=8) 
        {
            return "door to left (main passage continues straight for 10 ft)";
        }

        else if(roll<=11 && roll>=10) 
        {
            return "passage turns to left and continues for 10 ft";
        }

        else if(roll<=13 && roll>=12) 
        {
            return "passage turns to right and continues for 10 ft";
        }

        else if(roll<=16 && roll>=14) 
        {
            return "passage ends in door to chamber";
        }

        else if(roll==17) 
        {
            return "Stairs, (passage continues straight for 10 ft)";
        }

        else if (roll<=19 && roll>=18) 
        {
            return "Dead end";
        }

        else if(roll==20)
        {
            return "Wandering Monster (passage continues straight for 10 ft)";
        }

        else 
        {
            return null;
        }

3 个答案:

答案 0 :(得分:3)

这是一个非常好的问题。由于每个if-else都是相同的;您检查滚动是否在一定范围内,然后返回一个字符串,您可以使用枚举。相对于您正在处理并使用此数据表示的内容,可以改进枚举名称以及元素名称。

    public String generatePassageSection() {
        int roll = roll();

        Message message = Stream.of(Message.values()).filter(m -> m.inBounds(roll)).findAny().orElse(null);

        return message == null ? null : message.message;
    }

    enum Message {
        FIRST(1, 2, "passage goes straight for 10 feed."),
        SECOND(3, 5, "passage ends in the door to a chamber."),
        THIRD(6, 7, "door to right (main passage continues straight for 10 ft)"),
        FOURTH(9, 10, "door to left (main passage continues straight for 10 ft)"),
        FIFTH(10, 11, "passage turns to left and continues for 10 ft"),
        SIXTH(12, 13, "passage turns to right and continues for 10 ft"),
        SEVENTH(14, 16, "passage ends in door to chamber"),
        EIGHTH(17, 17, "Stairs, (passage continues straight for 10 ft)"),
        NINTH(18, 19, "Dead end"),
        TENTH(20, 20, "Wandering Monster (passage continues straight for 10 ft)");

        ;
        private final int minimumRollInclusive;

        private final int maximumRollInclusive;

        private final String message;

        Message(int minimumRollInclusive, int maximumRollInclusive, String message) {
            this.minimumRollInclusive = minimumRollInclusive;
            this.maximumRollInclusive = maximumRollInclusive;
            this.message = message;
        }

        boolean inBounds(int roll) {
            return roll >= minimumRollInclusive && roll <= maximumRollInclusive;
        }

    }

    int roll() {
        return 0; // use ur code
    }

此外,应注意,Enum#values每次调用时都会创建一个新的数组对象,因此值得对其进行缓存。

答案 1 :(得分:1)

优先使用数据结构而非代码。

final private static String text[] = {
    "passage goes straight for 10 feet.", // 1, 2
    "passage ends in door to a chamber.", // 3, 4, 5
    "door to right (main passage continues straight for 10 ft)", // 6, 7
    "door to left (main passage continues straight for 10 ft)", // 8, 9
    "passage turns to left and continues for 10 ft", // 10, 11
    "passage turns to right and continues for 10 ft", // 12, 13
    "passage ends in door to chamber", // 14, 15, 16
    "Stairs, (passage continues straight for 10 ft)", // 17
    "Dead end", // 18, 19
    "Wandering Monster (passage continues straight for 10 ft)" // 20
}

final private static int index[] = {
    0, 0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 8, 8, 9
}

public String generatePassageSection() {
    return text[index[roll()-1]];
}

手动构造的索引数组对文本数组的顺序和内容具有隐式假设。我认为在此例行程序的情况和规模下这是合理的;我不建议将其作为常规做法。

这隐含地假设已知roll()可靠地返回范围在1到20之间的结果;如果您不信任它,则应该添加错误检查。

答案 2 :(得分:1)

如果您只是想减少if语句的数量,则可以添加一系列可能的结果,将每个结果编入适当的骰子索引,然后在该索引处返回值。我不会为您全部写出来,但是看起来会像这样:

public String generatePassageSection(String[] diceRolls){
   return diceRolls[roll()];
}