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

时间:2020-03-15 12:43:58

标签: java if-statement conditional-statements refactoring

只是想知道是否有一种方法可以重构以下代码?我是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");
    }
  }
}

在此先感谢您的帮助!

3 个答案:

答案 0 :(得分:7)

您可以分别评估北部/南部和东部/西部的状况,并将方向粘贴到您的消息中。

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

我从您问题的代码中假设,您只关心这四个互补方向(不是正北,正东,静止等)。

答案 1 :(得分:1)

如果您真的想使其变干,可以使用运算符完成它吗?但是,既不容易阅读也不推荐。它用于编程竞赛,目标是尽可能快地完成比赛。

它遵循以下方案: (条件?WhatHappenIfConditionIsTrue:WhatHappenIfConditionIsFalse); 您可以在作业中使用它:

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"));
}

答案 2 :(得分: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");
                }*/
    }
}