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

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

如何使這個IF塊更短?

如何使這個IF塊更短?

catspeake 2021-04-11 16:43:27
如何使此代碼更短,更簡單?if ((fBody.equals("block") && sBody.equals("ball")) || (fBody.equals("ball") && sBody.equals("block"))) // CODEif ((fBody.equals("wall") && sBody.equals("bonus")) || (fBody.equals("bonus") && sBody.equals("wall"))) // CODE等等。
查看完整描述

2 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

您可以創建一個包含所有可能的“正確”對的列表,然后在其上循環:


class Pair {

    String sBody;

    String fBody;


    public String getsBody()

    {

        return sBody;

    }


    public String getfBody()

    {

        return fBody;

    }

}


boolean check(List<Pair> list, String sBody, String fBody) {

    for (Pair pair : list) {

        if (pair.getsBody().equals(sBody) && pair.getfBody().equals(fBody)) {

            return true;

        }

    }


    return false;

}

當然,您仍然必須以某種方式填充此列表


查看完整回答
反對 回復 2021-04-28
?
30秒到達戰場

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

我會將字面量“ ball”,“ wall”等放入Enum。


public enum ObjectType {


    BLOCK("block"),

    BALL("ball"),

    BONUS("bonus"),

    WALL("wall");


    private String objectType;


    ObjectType(String objectType) {

        this.objectType = objectType;

    }


    public String getObjectType() {

        return objectType;

    }


    public boolean equals(String body) {

        return objectType.equalsIgnoreCase(body);

    }

}

然后,我會繼續sBody和fBody一對。


public class Pair {


    private final String fBody;

    private final String sBody;


    public Pair(String fBody, String sBody) {

        this.fBody = fBody;

        this.sBody = sBody;

    }


    public String getfBody() {

        return fBody;

    }


    public String getsBody() {

        return sBody;

    }


    @Override

    public String toString() {

        return "Pair{" +

                "fBody='" + fBody + '\'' +

                ", sBody='" + sBody + '\'' +

                '}';

    }

}

然后,我將利用Java 8Predicate并創建謂詞列表,如下所示:


public final class Predicates {


    public static final List<Predicate<Pair>> PREDICATES =

            Arrays.asList(

                    isBlockBall(),

                    isBlockBall()

                    // add rest of your predicates

            );


    private Predicates() {

        // we do not need to instantiate this

    }


    public static Predicate<Pair> isWallBlock() {

        return p -> ObjectType.WALL.equals(p.getfBody())

                && ObjectType.BLOCK.equals(p.getsBody());

    }


    public static Predicate<Pair> isBlockBall() {

        return p -> ObjectType.BLOCK.equals(p.getfBody())

                && ObjectType.BALL.equals(p.getsBody());

    }

}

然后,您可以測試您的狀況,如下所示:


public class TestingPairs {


    public static void main(String[] args) {

        final String fBody = "block";

        final String sBody = "ball";

        Pair pair = new Pair(fBody, sBody);

        final Optional<Predicate<Pair>> conditionMet = PREDICATES.stream().filter(pairPredicate -> pairPredicate.test(pair))

                .findFirst();


        if (conditionMet.isPresent()) {

            // do your stuff

        }

    }

}


查看完整回答
反對 回復 2021-04-28
  • 2 回答
  • 0 關注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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