-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguess_num.html
More file actions
116 lines (100 loc) · 3.19 KB
/
guess_num.html
File metadata and controls
116 lines (100 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>猜数字游戏</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f2f2f2;
text-align: center;
padding: 50px;
color: #333;
}
h1 {
font-size: 48px;
font-weight: bold;
margin-bottom: 30px;
}
input {
width: 60px;
padding: 5px 10px;
margin-top: 10px;
border-radius: 4px;
border: 1px solid #ccc;
font-size: 18px;
text-align: center;
}
button {
margin-left: 10px;
padding: 6px 12px;
font-size: 18px;
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
transition-duration: 0.4s;
}
button:hover {
background-color: #45a049;
}
p {
font-size: 18px;
margin-top: 30px;
}
#message {
font-size: 24px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>猜数字游戏</h1>
<p>猜一个在1到100之间的数字。</p>
<p>请输入要猜测的范围:</p>
<label for="minRange">最小值:</label>
<input type="number" id="minRange" value="1">
<label for="maxRange">最大值:</label>
<input type="number" id="maxRange" value="100">
<button onclick="startGame()">开始游戏</button>
<br><br>
<input type="number" id="userGuess" min="1" max="100">
<button onclick="checkGuess()">提交</button>
<p id="message"></p>
<script>
let secretNumber;
let attempts = 0;
function startGame() {
let minRange = parseInt(document.getElementById("minRange").value);
let maxRange = parseInt(document.getElementById("maxRange").value);
if (minRange >= maxRange) {
showMessage("最小值必须小于最大值,请重新输入。");
return;
}
secretNumber = Math.floor(Math.random() * (maxRange - minRange + 1)) + minRange;
attempts = 0;
showMessage("游戏已开始,请输入您的猜测并点击提交。");
}
function checkGuess() {
if (secretNumber === undefined) {
showMessage("请先点击开始游戏。");
return;
}
let userGuess = parseInt(document.getElementById("userGuess").value);
attempts++;
if (userGuess === secretNumber) {
showMessage(`恭喜你,猜对了!尝试了${attempts}次。`);
} else if (userGuess < secretNumber) {
showMessage("太低了,请再试一次。");
} else {
showMessage("太高了,请再试一次。");
}
}
function showMessage(message) {
document.getElementById("message").innerHTML = message;
}
</script>
</body>
</html>