在java中按字符串<1.7切换案例

the*_*ava 2 javascript java switch-statement

function checkValue(value) {
    if (value === null || value === undefined || value === '') {
        return '-';
    } else {
        switch (value) {
        case true:
            return 'Yes';
            break;
        case false:
            return 'No';
            break;
        default:
            return value;
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道按字符串切换的情况只有> 1.7.无论如何我可以在不使用传统的java中转换这段代码if/else.这段代码是用来写的JavaScript.

Sur*_*tta 6

使用枚举代替<1.7开关字符串不支持

    private enum MyEnum {
        a, b, c, d;
    }

    String val; // input
    MyEnum mye = MyEnum.valueOf(val);

    switch (mye) {
        case a:
            return something;
        case carrot:
            return something;
        ..
    }
Run Code Online (Sandbox Code Playgroud)

PS:伪代码.IDE不在手.抱歉.