1

下面是一些 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!");
        }
    }
}
4

3 回答 3

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调用的重复。

于 2013-10-17T21:04:26.160 回答
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());
于 2013-10-17T21:47:39.173 回答
0
  1. 而不是if-else使用 if 语句来确定具体方向。
  2. 此外,由于每个方向都有两个选项“N”、“E”、“W”、“S”有一个方向条件,然后检查 x>0 或 x<0

例子;

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
    ..........
    ..........
}
于 2013-10-17T21:09:01.750 回答