我正在尝试制作一个骰子计数器,您可以在该骰子中滚动任意次数,当您打21时可以“赢”。如果您超过21,则代码将告诉您您输了,然后重试。我不知道如何拥有它,所以当总点击数达到21时,将会有不同的消息。这是到目前为止的代码:
var clicks = 0;
function random()
{
if (clicks > 21) {
alert("You Got To 22! You Lose! Please Try Again!");
location.reload();
}
clicks += Math.floor(Math.random() * 6) + 1;
document.getElementById("clicks").innerHTML = clicks;
};
<p> <button onclick="window.location.href='index.html'">Click Me To Go Back To The Rules!</button> </p>
<h2>Get To 21!</h2>
<div>
<p> Click The Dice To Role! The Counter Below Will Record Your Score!</p>
</div>
<div>
</div>
<div>
<p id="game"></p>
</div>
<div>
<input type="image" value="clicks" onclick="random()" src="https://www.propdog.co.uk/image/cache/data/accessories/dice/force-4-500x500.jpg" alt="Dice2" width="250" height="250">
</div>
<div>
Total Count: <a id="clicks">0</a>
</div>
答案 0 :(得分:1)
您需要做的就是添加一个else if
语句,并比较值是否为equal to 21
:
clicks += Math.floor(Math.random() * 6) + 1;
if (clicks > 21) {
alert("You Got To 22! You Lose! Please Try Again!");
location.reload();
} else if (clicks == 21) {
alert("You Got To 21! Good Job! You Win!");
location.reload();
}
document.getElementById("clicks").innerHTML = clicks;
答案 1 :(得分:0)
添加其他条件,检查分数是否== 21
var clicks = 0;
function random()
{
if (clicks > 21) {
alert("You Got To 22! You Lose! Please Try Again!");
location.reload();
} else if( clicks == 21 ) {
alert("You Got To 21! You Win! Want to play another?");
location.reload()
}
clicks += Math.floor(Math.random() * 6) + 1;
document.getElementById("clicks").innerHTML = clicks;
};
<p> <button onclick="window.location.href='index.html'">Click Me To Go Back To The Rules!</button> </p>
<h2>Get To 21!</h2>
<div>
<p> Click The Dice To Role! The Counter Below Will Record Your Score!</p>
</div>
<div>
</div>
<div>
<p id="game"></p>
</div>
<div>
<input type="image" value="clicks" onclick="random()" src="https://www.propdog.co.uk/image/cache/data/accessories/dice/force-4-500x500.jpg" alt="Dice2" width="250" height="250">
</div>
<div>
Total Count: <a id="clicks">0</a>
</div>