简体   繁体   English

在收到有效输入之前,如何继续执行JavaScript if .. else if语句?

[英]How can I continue a JavaScript if.. else if statement until I receive valid input?

How can I continue prompting a user for a valid response using if... else if statements? 如何使用if ... else if语句继续提示用户有效的响应? My script currently works once, but then breaks: 我的脚本目前只能运行一次,但随后会中断:

var enterNum = prompt("Please enter a number between 1 and 100", "");


if (isNaN(enterNum)){
    enterNum = prompt("You did not enter a valid number. Please try again", "")
}
else if (enterNum < 1 || enterNum >100){
    enterNum = prompt("Your number is not between 1 and 100. Please try again", "")
}
else{
    document.write("Your number is ", enterNum)
}

Thanks in advance! 提前致谢!

var enterNum = prompt("Please enter a number between 1 and 100", "");

while(isNaN(enterNum) || enterNum < 1 || enterNum >100) {
    enterNum = prompt("You did not enter a valid number. Please try again", "")
}
document.write("Your number is ", enterNum)

You can't with only if/else. 您不能仅当/否则。 Use a loop. 使用循环。 Example: 例:

var enterNum = prompt("Please enter a number between 1 and 100", "");
while(true)
{
  if (isNaN(enterNum)){
      enterNum = prompt("You did not enter a valid number. Please try again", "")
  }
  else if (enterNum < 1 || enterNum >100){
      enterNum = prompt("Your number is not between 1 and 100. Please try again", "")
  }
  else
    break;
}
document.write("Your number is ", enterNum)
var valid = false;
var msg = "Please enter a number between 1 and 100";

while(!valid){
    var enterNum = prompt(msg, "");

    if (isNaN(enterNum)){
        msg = "You did not enter a valid number. Please try again";
    }
    else if (enterNum < 1 || enterNum >100){
        msg = "Your number is not between 1 and 100. Please try again";
    }
    else{
        valid = true;
        document.write("Your number is ", enterNum)
    }
}

There are a bunch of other ways to do a similar thing, somewhat depending on style. 有很多其他方式可以做类似的事情,这在某种程度上取决于样式。 This went for readability. 这是为了提高可读性。 Could also eliminate the valid variable and simply have while(true) then break once the input is correct. 也可以消除valid变量,只需要while(true)然后在输入正确后就break The document.write could also be after the while. document.write也可能在片刻之后。

I hate javascript so my syntax is probably off but something like: 我讨厌javascript,因此我的语法可能已关闭,但类似:

var isValid = false;
var message = "Please enter a number between 1 and 100";

while(isValid == false)
{
    var enterNum = prompt(message, "");

    if (isNaN(enterNum)){
        isValid = false;
        message = "You did not enter a valid number. Please try again";
    }
    else if (enterNum < 1 || enterNum >100){
        isValid = false;
        message = "Your number is not between 1 and 100. Please try again";
    }
    else{
        isValid = true;
        document.write("Your number is ", enterNum)
    }
}

Using a do/while loop might be a little neater. 使用do / while循环可能会更整洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM