有没有一种方法可以重构这些if语句?

3

请问有没有一种方法可以重构以下代码?我是Java新手,尝试编写DRY代码-以下是我编写的内容,但似乎需要检查很多条件。

void printDirection() {
  if (yDirection > 0) {
    if (xDirection < 0) {
      println("Travelling South-West");
    } else {
      println("Travelling South-East");
    }
  } else if (yDirection < 0) {
    if (xDirection <0) {
      println("Travelling North-West");
    } else {
      println("Travelling North-East");
    }
  }
}

先行感谢任何的协助!


1
你可以将North/South部分和East/West部分分配给两个字符串,然后打印一个包含这两个字符串的消息。 - khelwood
3个回答

8

您可以单独评估南北和东西两个方向,并将它们融合到您的消息中。

System.out.printf("Travelling %s-%s%n", (yDirection < 0 ? "North" : "South"),
                  (xDirection < 0 ? "West" : "East"));

我根据您问题中的代码推测,您只关心这四个相互补充的方向(不是正北、正东、静止等方向)。


2
更好的方法是,可以使用枚举来编码 NORTH, SOUTH, WEST, EAST - Glains

1
如果你真的想让它变得DRY,可以使用运算符?,但这既不容易阅读,也不被推荐。它在编程竞赛中使用,目标是尽可能快地完成。

它遵循以下方案: (条件?如果条件为真会发生什么:如果条件为假会发生什么); 你可以在赋值中使用它:

int i = (a>0)?a:0;

在这种情况下,如果a>0,则i=a,否则a=0。

在你的情况下,我会这样做。

void printDirection()
{
    System.out.println("Travelling " + (yDirection > 0?"South":"North") + "-" + (xDirection>0?"East":"West"));
}

1
有人已经回答了这样的问题,但我很感激你解释为什么会这样编码!谢谢 :) - r0llie

0
一些建议: 1. 由于x,y的组合,有五种状态;您可以使用枚举类型来定义这些状态; 2. 如果您想在代码中减少if...else语句,请参考状态机设计模式;但我认为,在您的情况下,状态非常简单,不需要使其过于复杂。
public class Status {

    public enum Direction {
        SOUTH_WEST((x, y) -> y > 0 && x < 0, "Travelling South-West")
        , SOUTH_EAST((x, y) -> y >0 && x > 0, "Travelling South-East")
        , NORTH_EAST((x, y) -> x > 0 && y < 0, "Travelling North-East")
        , NORTH_WEST((x,y) -> x < 0 && y < 0, "Travelling North-West"), CENTER((x,y) -> x == 0 && y == 0, "");

        BiPredicate<Integer, Integer> bp;
        String desc;

        public BiPredicate<Integer, Integer> getBp() {
            return bp;
        }
        public void setBp(BiPredicate<Integer, Integer> bp) {
            this.bp = bp;

        }

        public String getDesc() {
            return desc;
        }
        public void setDesc(String desc) {
            this.desc = desc;
        }
        private Direction(BiPredicate<Integer, Integer> bp, String desc) {
            this.bp = bp;
            this.desc = desc;
        }
        public static Direction getDirection(int x, int y) {
            for (Direction direction : Direction.values()) {
                if(direction.getBp().test(x, y)) {
                    return direction;
                }
            }
            return null;
        }
    }

    public static void main(String[] args) {
        Direction d =  Direction.getDirection(3, 4);
        System.out.println(d.getDesc());
        /*      if(d == Direction.SOUTH_WEST){
                    System.out.println("do some thing");
                } else if(d == Direction.SOUTH_EAST){
                    System.out.println("do some thing");
                } else if(d == Direction.NORTH_EAST){
                    System.out.println("do some thing");
                } else if(d == Direction.NORTH_WEST){
                    System.out.println("do some thing");
                }*/
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接