如何减少这段代码中的 if else 语句?

这里的代码基本上用于根据骰子的滚动来决定字符串。其中有很多陈述,我确实需要一些帮助来减少这种情况。我在网上看了一些方法,但不太适合。


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;

        }


至尊宝的传说
浏览 60回答 3
3回答

大话西游666

这是一个非常好的问题。因为每个 if-else 都是一样的;您检查某个滚动是否在某个范围内,然后返回一个字符串,您可以只使用枚举。相对于您正在处理和用此数据表示的内容,可以改进枚举名称以及元素名称。&nbsp; &nbsp; public String generatePassageSection() {&nbsp; &nbsp; &nbsp; &nbsp; int roll = roll();&nbsp; &nbsp; &nbsp; &nbsp; Message message = Stream.of(Message.values()).filter(m -> m.inBounds(roll)).findAny().orElse(null);&nbsp; &nbsp; &nbsp; &nbsp; return message == null ? null : message.message;&nbsp; &nbsp; }&nbsp; &nbsp; enum Message {&nbsp; &nbsp; &nbsp; &nbsp; FIRST(1, 2, "passage goes straight for 10 feed."),&nbsp; &nbsp; &nbsp; &nbsp; SECOND(3, 5, "passage ends in the door to a chamber."),&nbsp; &nbsp; &nbsp; &nbsp; THIRD(6, 7, "door to right (main passage continues straight for 10 ft)"),&nbsp; &nbsp; &nbsp; &nbsp; FOURTH(9, 10, "door to left (main passage continues straight for 10 ft)"),&nbsp; &nbsp; &nbsp; &nbsp; FIFTH(10, 11, "passage turns to left and continues for 10 ft"),&nbsp; &nbsp; &nbsp; &nbsp; SIXTH(12, 13, "passage turns to right and continues for 10 ft"),&nbsp; &nbsp; &nbsp; &nbsp; SEVENTH(14, 16, "passage ends in door to chamber"),&nbsp; &nbsp; &nbsp; &nbsp; EIGHTH(17, 17, "Stairs, (passage continues straight for 10 ft)"),&nbsp; &nbsp; &nbsp; &nbsp; NINTH(18, 19, "Dead end"),&nbsp; &nbsp; &nbsp; &nbsp; TENTH(20, 20, "Wandering Monster (passage continues straight for 10 ft)");&nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; &nbsp; private final int minimumRollInclusive;&nbsp; &nbsp; &nbsp; &nbsp; private final int maximumRollInclusive;&nbsp; &nbsp; &nbsp; &nbsp; private final String message;&nbsp; &nbsp; &nbsp; &nbsp; Message(int minimumRollInclusive, int maximumRollInclusive, String message) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.minimumRollInclusive = minimumRollInclusive;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.maximumRollInclusive = maximumRollInclusive;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.message = message;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; boolean inBounds(int roll) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return roll >= minimumRollInclusive && roll <= maximumRollInclusive;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; int roll() {&nbsp; &nbsp; &nbsp; &nbsp; return 0; // use ur code&nbsp; &nbsp; }另外,应该注意的是,Enum#values 每次调用时都会创建一个新的数组对象,因此值得对其进行缓存。

郎朗坤

如果您只是想减少 if 语句的数量,您可以添加一组可能的结果,在适当的骰子上对每个结果进行索引,然后返回该索引处的值。我不会把它全部写给你,但它看起来有点像这样:public&nbsp;String&nbsp;generatePassageSection(String[]&nbsp;diceRolls){ &nbsp;&nbsp;&nbsp;return&nbsp;diceRolls[roll()]; }

子衿沉夜

更喜欢数据结构而不是代码。final private static String text[] = {&nbsp; &nbsp; "passage goes straight for 10 feet.", // 1, 2&nbsp; &nbsp; "passage ends in door to a chamber.", // 3, 4, 5&nbsp; &nbsp; "door to right (main passage continues straight for 10 ft)", // 6, 7&nbsp; &nbsp; "door to left (main passage continues straight for 10 ft)", // 8, 9&nbsp; &nbsp; "passage turns to left and continues for 10 ft", // 10, 11&nbsp; &nbsp; "passage turns to right and continues for 10 ft", // 12, 13&nbsp; &nbsp; "passage ends in door to chamber", // 14, 15, 16&nbsp; &nbsp; "Stairs, (passage continues straight for 10 ft)", // 17&nbsp; &nbsp; "Dead end", // 18, 19&nbsp; &nbsp; "Wandering Monster (passage continues straight for 10 ft)" // 20}final private static int index[] = {&nbsp; &nbsp; 0, 0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 8, 8, 9}public String generatePassageSection() {&nbsp; &nbsp; return text[index[roll()-1]];}手动构造的索引数组对文本数组的顺序和内容有隐式假设。就本次例行的情况和规模而言,我认为这是合理的;我不建议将其作为一般做法。这隐含地假设 roll() 已知能够可靠地返回 1 到 20(含)范围内的结果;如果你不能信任它,应该添加错误检查。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java