当前位置:首页 > 编程笔记 > 正文
已解决

数字闯关游戏HTML代码

来自网友在路上 153853提问 提问时间:2023-09-24 10:54:56阅读次数: 53

最佳答案 问答题库538位专家为你答疑解惑

代码如下


<!DOCTYPE html>
<html>
<head>
    <title>算数闯关游戏</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            
        }
        .container {
            text-align: center;
            margin-top: 100px;
        }
        .btn {
            background-color: green;
            color: white;
            padding: 10px 20px;
            font-size: 18px;
            border: none;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>计算题APP</h2>
        <p>请回答以下题目:</p>
        <p id="question"></p>
        <input type="number" id="answer" placeholder="请输入答案">
        <button class="btn" οnclick="checkAnswer()">提交答案</button>
        <p id="score">得分: 0</p>
    </div>

    <script>
        var score = 0;

        function generateQuestion() {
            var num1 = Math.floor(Math.random() * 100);
            var num2 = Math.floor(Math.random() * 100);
            var operator = Math.floor(Math.random() * 4);
            var questionString = "";

            switch (operator) {
                case 0:
                    questionString = num1 + " + " + num2;
                    break;
                case 1:
                    questionString = num1 + " - " + num2;
                    break;
                case 2:
                    questionString = num1 + " * " + num2;
                    break;
                case 3:
                    questionString = num1 + " / " + num2;
                    break;
            }

            document.getElementById("question").innerHTML = questionString;
        }

        function checkAnswer() {
            var userAnswer = parseInt(document.getElementById("answer").value);
            var question = document.getElementById("question").innerHTML;
            var correctAnswer = eval(question);

            if (userAnswer === correctAnswer) {
                score += 1;
            } else {
                score -= 2;
            }

            document.getElementById("score").innerHTML = "得分: " + score;
            document.getElementById("answer").value = "";

            generateQuestion();
        }

        generateQuestion();
    </script>
</body>
</html>

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"数字闯关游戏HTML代码":http://eshow365.cn/6-12730-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!