if-else和switch语句的替代方案

Shr*_*ani 14 javascript java conditional-statements

我在Java中有以下代码:

public void doSomething(int i) {
    if (i == 12) {
        // order should be same
        up();
        left();
        stop();
    }
    if (i == 304) {
        // order should be same
        right();
        up();
        stop();
    }
    if (i == 962) {
        // order should be same
        down();
        left();
        up();
        stop();
    }
}
// similar code can be done using switch case statements.
// all the function can have any functionality and might not resemble to the name given to them.
Run Code Online (Sandbox Code Playgroud)

现在,如果我被要求不使用if-else和switch case语句,那么可以做些什么呢?代码可以用Java或JavaScript完成.

Qan*_*avy 27

如果您可以使用JavaScript,则可以使用具有以下功能的对象:

function doSomething(i) {
  var obj = {};

  obj[12] = function () {
    // order should be same
    up();
    left();
    stop();
  };
  obj[304] = function () {
    // order should be same
    right();
    up();
    stop();
  };
  obj[962] = function () {
    // order should be same
    down();
    left();
    up();
    stop();
  };

  // apparently we can't use any conditional statements
  try {
    obj[i]();
  } catch (e) {}
}
Run Code Online (Sandbox Code Playgroud)

如果仅允许ifswitch语句,则if用逻辑AND运算符(&&)替换所有语句:

function doSomething(i) {
  (i == 12) && (
    // order should be same
    up(),
    left(),
    stop()
  );

  (i == 304) && (
    // order should be same
    right(),
    up(),
    stop()
  );

  (i == 962) && (
    // order should be same
    down(),
    left(),
    up(),
    stop()
  );
}
Run Code Online (Sandbox Code Playgroud)

  • 而不是try/catch,我个人认为这更具可读性:`obj [i] && obj [i]();` (2认同)

Sal*_*n A 16

以下是在JavaScript中完成此操作的简单方法:

function up()    { console.log("up");    }
function down()  { console.log("down");  }
function left()  { console.log("left");  }
function right() { console.log("right"); }
function stop()  { console.log("stop");  }

var fnmaps = {
    12:  [up, left, stop],
    304: [right, up, stop],
    962: [down, left, up, stop]
};

function doSomething(i) {
    var fnmap = fnmaps[i] || [], j;
    for (j = 0; j < fnmap.length; j++) {
        fnmap[j]();
    }
}

doSomething(12);
doSomething(304);
doSomething(962);
Run Code Online (Sandbox Code Playgroud)

只需编辑map变量即可添加/排序函数.


ysh*_*vit 12

您可以制作输入操作的字典.在Java中,这将是一个Map<Integer, Runnable>,例如*:

map.put(12, () -> {
    up();
    left();
    stop();
});
Run Code Online (Sandbox Code Playgroud)

然后,您可以获得适当的Runnable并运行它:

Runnable action = map.get(i);
if (action != null) {
    action.run();
} else {
    // some default action
    // This is the "default" case in a switch, or the "else" in an if-else
}
Run Code Online (Sandbox Code Playgroud)

if-else并不是绝对必要的,但没有它,你会得到一个NullPointerExceptionif i不是一个期望的值 - 也就是你放入地图的一个值.

这个想法在JavaScript中类似,但是使用对象而不是Maps,函数(可能是匿名的)而不是Runnables等.


此代码适用于Java 8.在Java 7及更低版本中,您将执行以下操作:

map.put(12, new Runnable() {
    @Override
    public void run() {
        up();
        left();
        stop();
    }
});
Run Code Online (Sandbox Code Playgroud)


小智 5

娃哈哈哈哈,你救了我的一天:)这应该工作,现在没办法测试..

public void doSomething(int i) {

try {
    int x = 1/(12-i); // fails for i==12
} catch (ArithmeticException e) {
        up();
        left();
        stop();
}
Run Code Online (Sandbox Code Playgroud)

等等,享受!