带有数字的 Else If 语句

时间:2021-07-19 10:02:01

标签: javascript if-statement

我的代码似乎不起作用,我不知道为什么,我知道 else if 语句,所以也许我这样做完全错误。我需要用户输入一个数字,然后根据他们输入的数字打印出一条语句。

请看下面我的代码,在用户输入数字并按回车键后,我得到的错误消息是“未定义”。

let waterPay = prompt("Please enter the amount of water you use to get a price you need to pay, thank you!");

if (waterPay > 6000) {
  console.log("The number is below 6000");
} else if (waterPay(6000 <= 10500)) {
  console.log("The number is between 6000 and 10500");
} else if (waterPay(10.5 <= 35000)) {
  console.log("The number is between 10500 and 35000");
} else(waterPay( <= 35000)) {
  console.log("The number is above 35000");
}

3 个答案:

答案 0 :(得分:0)

粗略看,if-else-if 的结构似乎是正确的。不过,您的比较似乎存在问题。例如 waterPay (6000 <= 10500) 是无效的语法。你可能想写 waterPay > 6000 && waterPay < 10500

还要确保所有分支都可以访问,即条件不重叠

答案 1 :(得分:0)

查看 if...else page on mdn 以查看预期的语法。

let waterPay = prompt("Please enter the amount of water you use to get a price you need to pay, thank you!");

if (waterPay < 6000) {
  console.log("The number is below 6000");
} else if (waterPay <= 10500) {
  console.log("The number is between 6000 and 10500");
} else if (waterPay <= 35000) {
  console.log("The number is between 10501 and 35000");
} else {
  console.log("The number is above 35001");
}

答案 2 :(得分:-1)

let waterPay = prompt("Please enter the amount of water you use to get a price you need to pay, thank you!");

if (waterPay < 6000) {
  console.log("The number is below 6000");
} else if (waterPay > 6000 && waterPay <= 10500)) {
  console.log("The number is between 6000 and 10500");
} else if (waterPay > 10500 && waterPay <= 35000) {
  console.log("The number is between 10500 and 35000");
} else(waterPay <= 35000) {
  console.log("The number is above 35000");
}

相关问题