<!DOCTYPE html>
<html>
  <head>
    <title>猜数字游戏</title>
    <style>
      body {
        text-align: center;
      }
      h1 {
        color: #333;
      }
      p {
        color: #777;
      }
      input {
        padding: 5px;
        margin: 10px;
      }
      button {
        padding: 10px 20px;
        background-color: #333;
        color: #fff;
        border: none;
        cursor: pointer;
      }
      #result {
        margin-top: 20px;
      }
    </style>
  </head>
  <body>
    <h1>猜数字游戏</h1>
    <p>猜一个1到100之间的随机数字!</p>
    <input type="number" id="guess" min="1" max="100" />
    <br />
    <button onclick="checkGuess()">猜</button>
    <p id="result"></p>
<pre><code>&lt;script&gt;
  var targetNumber = Math.floor(Math.random() * 100) + 1;
  var attempts = 0;

  function checkGuess() {
    var guess = parseInt(document.getElementById(&quot;guess&quot;).value);

    if (guess === targetNumber) {
      document.getElementById(&quot;result&quot;).innerHTML =
        &quot;恭喜你,猜对了!一共猜了&quot; + attempts + &quot;次。&quot;;
    } else if (guess &lt; targetNumber) {
      document.getElementById(&quot;result&quot;).innerHTML = &quot;太小了!再试一次。&quot;;
      attempts++;
    } else {
      document.getElementById(&quot;result&quot;).innerHTML = &quot;太大了!再试一次。&quot;;
      attempts++;
    }
  }
&lt;/script&gt;
</code></pre>
  </body>
</html>
这是一个简单的猜数字游戏,玩家需要在输入框中输入一个1到100之间的数字,然后点击“猜”按钮来猜测。游戏会根据玩家的猜测给出相应的提示,直到猜中目标数字为止。页面会显示玩家一共猜了多少次才猜中。

原文地址: https://www.cveoy.top/t/topic/pRcQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录