猿问

如何使这个IF块更短?

如何使此代码更短,更简单?


if ((fBody.equals("block") && sBody.equals("ball")) || (fBody.equals("ball") && sBody.equals("block"))) // CODE

if ((fBody.equals("wall") && sBody.equals("bonus")) || (fBody.equals("bonus") && sBody.equals("wall"))) // CODE

等等。


catspeake
浏览 154回答 2
2回答

GCT1015

您可以创建一个包含所有可能的“正确”对的列表,然后在其上循环:class Pair {&nbsp; &nbsp; String sBody;&nbsp; &nbsp; String fBody;&nbsp; &nbsp; public String getsBody()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return sBody;&nbsp; &nbsp; }&nbsp; &nbsp; public String getfBody()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return fBody;&nbsp; &nbsp; }}boolean check(List<Pair> list, String sBody, String fBody) {&nbsp; &nbsp; for (Pair pair : list) {&nbsp; &nbsp; &nbsp; &nbsp; if (pair.getsBody().equals(sBody) && pair.getfBody().equals(fBody)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}当然,您仍然必须以某种方式填充此列表

30秒到达战场

我会将字面量“ ball”,“ wall”等放入Enum。public enum ObjectType {&nbsp; &nbsp; BLOCK("block"),&nbsp; &nbsp; BALL("ball"),&nbsp; &nbsp; BONUS("bonus"),&nbsp; &nbsp; WALL("wall");&nbsp; &nbsp; private String objectType;&nbsp; &nbsp; ObjectType(String objectType) {&nbsp; &nbsp; &nbsp; &nbsp; this.objectType = objectType;&nbsp; &nbsp; }&nbsp; &nbsp; public String getObjectType() {&nbsp; &nbsp; &nbsp; &nbsp; return objectType;&nbsp; &nbsp; }&nbsp; &nbsp; public boolean equals(String body) {&nbsp; &nbsp; &nbsp; &nbsp; return objectType.equalsIgnoreCase(body);&nbsp; &nbsp; }}然后,我会继续sBody和fBody一对。public class Pair {&nbsp; &nbsp; private final String fBody;&nbsp; &nbsp; private final String sBody;&nbsp; &nbsp; public Pair(String fBody, String sBody) {&nbsp; &nbsp; &nbsp; &nbsp; this.fBody = fBody;&nbsp; &nbsp; &nbsp; &nbsp; this.sBody = sBody;&nbsp; &nbsp; }&nbsp; &nbsp; public String getfBody() {&nbsp; &nbsp; &nbsp; &nbsp; return fBody;&nbsp; &nbsp; }&nbsp; &nbsp; public String getsBody() {&nbsp; &nbsp; &nbsp; &nbsp; return sBody;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Pair{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "fBody='" + fBody + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ", sBody='" + sBody + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';&nbsp; &nbsp; }}然后,我将利用Java 8Predicate并创建谓词列表,如下所示:public final class Predicates {&nbsp; &nbsp; public static final List<Predicate<Pair>> PREDICATES =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isBlockBall(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isBlockBall()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add rest of your predicates&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; private Predicates() {&nbsp; &nbsp; &nbsp; &nbsp; // we do not need to instantiate this&nbsp; &nbsp; }&nbsp; &nbsp; public static Predicate<Pair> isWallBlock() {&nbsp; &nbsp; &nbsp; &nbsp; return p -> ObjectType.WALL.equals(p.getfBody())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && ObjectType.BLOCK.equals(p.getsBody());&nbsp; &nbsp; }&nbsp; &nbsp; public static Predicate<Pair> isBlockBall() {&nbsp; &nbsp; &nbsp; &nbsp; return p -> ObjectType.BLOCK.equals(p.getfBody())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && ObjectType.BALL.equals(p.getsBody());&nbsp; &nbsp; }}然后,您可以测试您的状况,如下所示:public class TestingPairs {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; final String fBody = "block";&nbsp; &nbsp; &nbsp; &nbsp; final String sBody = "ball";&nbsp; &nbsp; &nbsp; &nbsp; Pair pair = new Pair(fBody, sBody);&nbsp; &nbsp; &nbsp; &nbsp; final Optional<Predicate<Pair>> conditionMet = PREDICATES.stream().filter(pairPredicate -> pairPredicate.test(pair))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst();&nbsp; &nbsp; &nbsp; &nbsp; if (conditionMet.isPresent()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // do your stuff&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答