亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何減少這段代碼中的 if else 語句?

如何減少這段代碼中的 if else 語句?

至尊寶的傳說 2024-01-05 10:52:54
這里的代碼基本上用于根據骰子的滾動來決定字符串。其中有很多陳述,我確實需要一些幫助來減少這種情況。我在網上看了一些方法,但不太適合。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 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

這是一個非常好的問題。因為每個 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 每次調用時都會創建一個新的數組對象,因此值得對其進行緩存。


查看完整回答
反對 回復 2024-01-05
?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

如果您只是想減少 if 語句的數量,您可以添加一組可能的結果,在適當的骰子上對每個結果進行索引,然后返回該索引處的值。我不會把它全部寫給你,但它看起來有點像這樣:

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


查看完整回答
反對 回復 2024-01-05
?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

更喜歡數據結構而不是代碼。


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(含)范圍內的結果;如果你不能信任它,應該添加錯誤檢查。


查看完整回答
反對 回復 2024-01-05
  • 3 回答
  • 0 關注
  • 183 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號