• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

简单的猜谜游戏

JavaScript 来源:Ryan Robertson 6次浏览

我想做一个简单的猜谜游戏。我无法获得正确操作的功能,或将消息显示给用户。我没有正确使用innerHTML吗?当数字被正确猜测时,我也希望游戏重新加载,我不确定它是否有效,因为游戏无法运行。简单的猜谜游戏

<!DOCTYPE html> 
<html> 
    <head lang="en"> 
     <meta charset="utf-8"> 
     <title>Guess the Number</title> 
     <link rel="stylesheet" href="css/Lab6.css" /> 
     <script type="text/javascript" src="script/Lab6.js"></script> 
    </head> 
    <body onload="pickInteger()"> 
     <div> 
     <h2><strong>Guess the Number</strong></h2> 
     </div> 
     <br/> 
     <div id="formDiv"> 
     <form name="AForm" method="get" > 
      <p>The computer has picked a number between 1 - 99, you must choose the correct number to win the game. When you guess the right number the game will restart.<br/> 
      </p> 
      <div id="bodyDiv"> 
       <p> Your guess is: 
        <input id="guess" type="text" size="1" name="theData" value="" autofocus/> 
        <input type="button" name="mybutton" value=" Guess " onclick="checkGuess()"> 
       </p> 
       <p id="output"> 
       </p> 
      </div> 
     </form> 
     </div> 
    </body> 
</html> 

var number = 0; 
var output = document.getElementById("output").innerHTML; 

function pickInteger() { 
    "use strict"; 
    number = Math.floor(Math.random() * 10 + 1); 
} 

function checkGuess() { 
    "use strict"; 

    var guess = document.getElementById("guess").value; 
     if (guess == number) { 
      alert(number + " " + "Is the correct number!"); 
      output = ""; 
      pickInteger(); 
     } 
     if (guess < number); { 
      output = "The number I am thinking of is higher than" + guess; 
     } 
     else if (guess > number); { 
      output = "The number I am thinking of is lower than" + guess; 
     } 
} 


===========解决方案如下:

你有一个分号if (guess < number);else if (guess > number);这是不对的只是删除它,它就会开始工作,请参见下面的代码


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)