while while循环不起作用提示。使用Javascript

时间:2017-02-26 15:22:01

标签: javascript do-while prompt

我正在使用JS Bin网站撰写此文章。

当我输入错误的(数字)输入时,循环仅触发一次:

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


  ....
}

2 个答案:

答案 0 :(得分:-2)

将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);


  ....
}

答案 1 :(得分:-2)

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


      ....
    }