2017-02-26 106 views
0

我正在使用JS Bin网站来写这个。做while循环时不提示。 Javascript

循环火灾只有当我给个差(数字)输入一次:

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

的'prompt'函数返回一个'string',而不是一个'number'。您可以查看['parseInt'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt)函数。 –

+1

您需要添加一个'else {inputOk = true; '',否则你的代码对我来说工作得很好。 – 4castle

+0

@DarinDimitrov是正确的。使用parseInt函数。 year = parseInt(输入)。 –

回答

-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

代码转储不是有用的答案。说*你改变了什么,以及*为什么*。 –

+0

@ T.J.Crowder谢谢你不知道。我编辑了答案。 – MotKohn

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


     .... 
    } 
+0

代码转储不是有用的答案。说*你做了什么,以及*为什么*。 (并合理地格式化代码。) –

+0

另请注意,您已更改范围。您不能只是将'year <0'更改为'year> 0'来反转该条件。这是'year> = 0'。 –