我只是想知道是否有更好的方法来解决我的问题:
我有6个自变量来检查.但如果任何条件成立,则不应该检查其他条件.通常我会写:
if (cond1 ) {
statement
} else {
if ( cond2 ) {
statement
} else {
if (cond3) {
statement
} else {
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
当然你会承认它看起来不好或者虽然它有效但也不容易阅读.你是否知道其他任何方式来编写if语句可能使用其他符号或函数(切换?而?)
是的,你可以做到
if (cond1 ) {
statement
} elseif ( cond2 ) {
statement
} elseif ( cond3 ) {
statement
}
Run Code Online (Sandbox Code Playgroud)
见文档