如何将此嵌套的if-else语句转换为非嵌套的if-else if-else语句?您可能需要添加一些布尔运算符,以使其完全非嵌套:
if (ball > 0) {
if (cup > 0) {
console.log(“I have a ball and cup.”);
} else {
console.log(“I have a ball.”);
}
} else {
if (cup > 0) {
console.log(“I have a cup”);
} else {
console.log(“I have nothing”);
}
}发布于 2018-11-06 11:45:48
如果我理解你想要做什么,那么这可能会有所帮助:
if (ball > 0 && cup > 0) {
console.log(“I have a ball and cup.”);
} else if (ball > 0) {
console.log(“I have a ball.”);
}
if (ball <= 0 && cup > 0) {
console.log(“I have a cup”);
} else if (ball <= 0) {
console.log(“I have nothing”);
}您可以只检查每个if语句中的多个条件,而不是嵌套if-else语句。
https://stackoverflow.com/questions/53165391
复制相似问题