【发布时间】:2015-07-06 07:59:14
【问题描述】:
我正在编写一个游戏,但我遇到了这个问题:最后一个 if 语句(及其所有 else if 语句)永远不会执行。
这是无效的代码:
const compare = prompt("1.mars, 2.jupiter, 3.moon")
if (compare === 2) {
confirm("your airplane crashed and you died")
} else if (compare === 1) {
confirm("you arrived safely")
} else if (compare === 3) {
confirm("you survived an airplane crash but you need to escape")
}
【问题讨论】:
-
prompt查询一个字符串,但你与整数比较(例如:'1' === 1等价于false)跨度> -
您可以将 if 语句更改为仅使用
==而不是===,因此类型也不必匹配:if (compare == 2)... 等
标签: javascript if-statement prompt