来一个四则运算游戏的网站代码

<p>下面是一份使用 HTML、CSS 和 JavaScript 实现四则运算游戏的代码示例:</p>
<p>```
<!DOCTYPE html></p>
<html>
  <head>
    <meta charset="UTF-8">
    <title>四则运算游戏</title>
    <style>
      /* 这里写 CSS 样式 */
      #game {
        width: 500px;
        margin: 0 auto;
        text-align: center;
      }
      #question {
        font-size: 20px;
        margin-top: 50px;
      }
      #answer {
        margin-top: 20px;
      }
      #result {
        margin-top: 20px;
        color: green;
      }
    </style>
  </head>
  <body>
    <div id="game">
      <div id="question">
        <p id="firstNumber"></p>
        <p id="operator"></p>
        <p id="secondNumber"></p>
        <p>=</p>
      </div>
      <div id="answer">
        <input type="text" id="inputAnswer">
        <button id="submit">提交</button>
      </div>
      <div id="result"></div>
    </div>
    <script>
      // 这里写 JavaScript 代码
      const firstNumber = document.getElementById("firstNumber");
      const operator = document.getElementById("operator");
      const secondNumber = document.getElementById("secondNumber");
      const inputAnswer = document.getElementById("inputAnswer");
      const submit = document.getElementById("submit");
      const result = document.getElementById("result");

<pre><code>  // 随机生成数字和运算符
  function generate() {
    firstNumber.textContent = Math.floor(Math.random() * 100);
    secondNumber.textContent = Math.floor(Math.random() * 100);
    const operators = [&quot;+&quot;, &quot;-&quot;, &quot;×&quot;, &quot;÷&quot;];
    operator.textContent = operators[Math.floor(Math.random() * 4)];
  }

  // 检查答案是否正确
  function checkAnswer() {
    const a = parseInt(firstNumber.textContent);
    const b = parseInt(secondNumber.textContent);
    const op = operator.textContent;
    let c;
    switch (op) {
      case &quot;+&quot;:
        c = a + b;
        break;
      case &quot;-&quot;:
        c = a - b;
        break;
      case &quot;×&quot;:
        c = a * b;
        break;
      case &quot;÷&quot;:
        c = a / b;
        break;
    }
    if (inputAnswer.value == c) {
      result.textContent = &quot;正确!&quot;;
    } else {
      result
</code></pre>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值