简体   繁体   English

执行while循环无法使用提示。 Java脚本

[英]Do while loop not working with prompt. Javascript

I'm using JS Bin website to write this. 我正在使用JS Bin网站编写此代码。

the loop fires only once when I give a bad (numeric) input : 输入错误的(数字)输入时,循环仅触发一次:

function isInputLeapYear()
{
    var year = -1;

    var inputOk = true;

    do{
       year = prompt("Please enter a year to check if it is a leap year \ninput year between 0-9999");

       if(year < 0 || 9999 < year) // check input
       {
         inputOk = false;
         alert("\""+year+"\" is not a good year. \nThe input needs to be between 0-9999");              
       };


    }while(inputOk === false);


  ....
}

Move the the var inoutOK = true into the loop that way it resets before prompting again. 将var inoutOK = true移到循环中,以使其重置,然后再次提示。

function isInputLeapYear()
{
   var year = -1;

   do {
       var inputOk = true;
       year = prompt("Please enter a year to check if it is a leap year \ninput year between 0-9999");

       if(year < 0 || 9999 < year) // check input
       {
          inputOk = false;
          alert("\""+year+"\" is not a good year. \nThe input needs to be between 0-9999");              
       };


    } while(inputOk === false);


  ....
}
    function isInputLeapYear()
    {
        var year = -1;

        var inputOk = false;

        do{
           year = prompt("Please enter a year to check if it is a leap year \ninput year between 0-9999");

           if(year > 0 && 9999 > year) // check input
           {
             inputOk = True;



           else
{
alert("\""+year+"\" is not a good year. \nThe input needs to be between 0-9999"); 
}  
           };


        }while(inputOk === false);


      ....
    }

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

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