来一个四则运算游戏的网站代码
时间: 2023-02-26 21:03:11 浏览: 50
下面是一份使用 HTML、CSS 和 JavaScript 实现四则运算游戏的代码示例:
```
<!DOCTYPE html>
<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");
// 随机生成数字和运算符
function generate() {
firstNumber.textContent = Math.floor(Math.random() * 100);
secondNumber.textContent = Math.floor(Math.random() * 100);
const operators = ["+", "-", "×", "÷"];
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 "+":
c = a + b;
break;
case "-":
c = a - b;
break;
case "×":
c = a * b;
break;
case "÷":
c = a / b;
break;
}
if (inputAnswer.value == c) {
result.textContent = "正确!";
} else {
result
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)