if-else 和 switch 语句的替代方案
2022-09-03 16:22:56
						我在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.
现在,如果我被要求不使用 if-else 和 switch case 语句中的任何一个,那么可以做些什么呢?代码可以在Java或JavaScript中完成。