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

发布于 2024-11-28 07:16:45 字数 466 浏览 2 评论 0原文

如何使用 if...else if 语句继续提示用户提供有效响应?我的脚本目前可以运行一次,但随后就会中断:

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)
}

提前致谢!

How can I continue prompting a user for a valid response using if... else if statements? 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凝望流年 2024-12-05 07:16:45
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)
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)
或十年 2024-12-05 07:16:45
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)
    }
}

还有很多其他方法可以完成类似的事情,具体取决于风格。这是为了可读性。还可以消除 valid 变量,只需使用 while(true),然后在输入正确后breakdocument.write 也可能会在一段时间之后。

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. The document.write could also be after the while.

倾城花音 2024-12-05 07:16:45

你不能只用 if/else。使用循环。例子:

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)

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)
还不是爱你 2024-12-05 07:16:45

我讨厌 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)
    }
}

使用 do/while 循环可能会更整洁一些。

I hate javascript so my syntax is probably off but something like:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文