如果声明重复缩短

时间:2013-10-17 20:57:00

标签: java if-statement

下面是一些if语句,用于根据他在地图上的位置以及他所面对的方向为机器人传递正确的方向。 有一点如果重复,但当我试图结合条件时,程序停止工作。 有什么提示可以缩短这个吗?

        int changeX= x-lowest.getX();
        int changeY= y-lowest.getY();
        if(changeX!=0){
            System.out.println("x changed");
            if(changeX<0 && robot.getFacing()== 'N'){
                robot.checkDirection('L','W');
            }
            else if(changeX<0 && robot.getFacing()== 'W'){
                robot.checkDirection('L','W');
            }else if(changeX<0 && robot.getFacing()== 'S'){
                robot.checkDirection('R','W');  
            }else if(changeX<0 && robot.getFacing()== 'E'){
                robot.checkDirection('R','W');  
            }else if (changeX>0 && robot.getFacing()== 'S'){
                robot.checkDirection('L','E');
            }else if (changeX>0 && robot.getFacing()== 'E'){
                robot.checkDirection('L','E');
            }else{
                robot.checkDirection('R', 'E');
            }           
        }else{
            System.out.println("y changed");
            if(changeY<0 && robot.getFacing()== 'N'){
                robot.checkDirection('L','S');
            }else if(changeY<0 && robot.getFacing()== 'W'){
                robot.checkDirection('L','S');
            }else if(changeY<0 && robot.getFacing()== 'E'){
            robot.checkDirection('R','S');  
            }else if(changeY<0 && robot.getFacing()== 'S'){
                robot.checkDirection('R','S');  
            }else if (changeY>0 && robot.getFacing()== 'S'){
            robot.checkDirection('L','N');
            }else if (changeY>0 && robot.getFacing()== 'E'){
                robot.checkDirection('L','N');
            }else{
                robot.checkDirection('R','N');
            }
        }
        //change the current cell and the x/y
        currentCell = lowest;
        x=lowest.getX();
        y=lowest.getY();

        if(goal()){
            moving=false;
            System.out.println("Goal Achieved!");
        }
    }
}

3 个答案:

答案 0 :(得分:2)

首先,您至少可以提取robot.getFacing()来电,这样您就不会重复它了。然后,您可以将一些语句与||结合使用:

String facingDirection = robot.getFacing();

if (changeX != 0){
    System.out.println("x changed");

    if(changeX<0 && (facingDirection == 'N' || facingDirection == 'W')) {
        robot.checkDirection('L','W');
    }else if(changeX<0 && (facingDirection == 'S' || facingDirection == 'E')) {
        robot.checkDirection('R','W');
    }else if (changeX>0 && (facingDirection == 'S' || facingDirection == 'E')) {
        robot.checkDirection('L','E');
    }else{
        robot.checkDirection('R', 'E');
    }
} else {
    System.out.println("y changed");

    if(changeY<0 && (facingDirection== 'N' || facingDirection== 'W')) {
        robot.checkDirection('L','S');
    }else if(changeY<0 && (facingDirection== 'E' || facingDirection== 'S')) {
        robot.checkDirection('R','S');
    }else if (changeY>0 && (facingDirection== 'S' || facingDirection== 'E')) {
        robot.checkDirection('L','N');
    }else{
        robot.checkDirection('R','N');
    }
}

如果你想更进一步,你可以将参数(例如'R'和'N')提取到两个单独的变量中,然后将它们全部设置在最后。这将揭示数据中的一些可能模式,并减少checkDirection调用的重复。

答案 1 :(得分:0)

  
      
  1. 而不是if-else使用if语句来确定具体方向。
  2.   
  3. 此外,由于您可以为每个方向选择两个选项:&#39; N&#39;&#39; E&#39;,&#39; W&#39;&#39; S&#39;具有该方向的条件,然后检查x> 0或x <0
  4.   

实施例

if(changeX!=0){
    if(getDirection() == 'N'){
          if(x>=getX()){
               System.out.println("N Direction");
          }else{
               System.out.println("S Direction");
          }
    }

    // for East
     if(getDirection() == 'E'){
          if(x>=getX()){
               System.out.println("N Direction");
          }else{
               System.out.println("S Direction");
          }
    }

    // for West
    ..........
    ..........

    // for South
    ..........
    ..........
}

// Here you remove the if-else since this will be 
// more clear and maintainable.
// If you have a huge logic, in if(){} by the time you come to else, 
// you need not go back to check what was the if condition.
// One more benefit of this is if they are isolated, and independent task, you 
// can encapsulate the logic in a method. 

if(changeY!=0){
    if(getDirection() == 'N'){
          if(x>=getX()){
               System.out.println("N Direction");
          }else{
               System.out.println("S Direction");
          }
    }

    // for East
     if(getDirection() == 'E'){
          if(x>=getX()){
               System.out.println("N Direction");
          }else{
               System.out.println("S Direction");
          }
    }

    // for West
    ..........
    ..........

    // for South
    ..........
    ..........
}

答案 2 :(得分:0)

我会创建一些易于阅读和修改的配置并使用它,而不使用if语句。

一个简单的例子就是说明这个概念:

    Map<ConfigKey, Pair<Character, Character>> config = new HashMap<ConfigKey, Pair<Character, Character>>();
    config.put(new ConfigKey("X", "DEC", "NW"), Pair.of('L', 'W'));
    config.put(new ConfigKey("X", "DEC", "ES"), Pair.of('R', 'W'));
    config.put(new ConfigKey("X", "INC", "NW"), Pair.of('R', 'E'));
    config.put(new ConfigKey("X", "INC", "ES"), Pair.of('L', 'E'));

    config.put(new ConfigKey("Y", "DEC", "NW"), Pair.of('L', 'S'));
    config.put(new ConfigKey("Y", "DEC", "ES"), Pair.of('R', 'S'));
    config.put(new ConfigKey("Y", "INC", "NW"), Pair.of('R', 'N'));
    config.put(new ConfigKey("Y", "INC", "ES"), Pair.of('L', 'N'));

    String axis   = (changeX != 0) ? "X" : "Y";
    int change    = (changeX != 0) ? changeX : changeY;
    String incDec = (change   > 0) ? "INC" : "DEC";
    String direction = (Arrays.asList('N','W').contains(facingDirection)) ? "NW" : "ES";

    Pair<Character, Character> result = config.get(new ConfigKey(axis, incDec, direction));
    robot.checkDirection(result.getLeft(), result.getRight());