【发布时间】:2018-06-25 12:03:04
【问题描述】:
我正在制作一个简单的二十一点游戏,但我的 if 有问题 陈述。当我单击触发 if 语句的站立按钮时,无论分数是多少,它总是显示提示“你赢了”。
例如,如果用户得分为 11,而计算机得分为 18(它始终设置为),则应显示提示“你输了”。
var randomnumber = Math.floor(Math.random() * 10 + 1);
function random() {
return randomnumber;
}
var total = randomnumber;
function dealcard() {
total += Math.floor(Math.random() * 10 + 1);
document.getElementById('playerscards').value = total;
if (total > 21) {
alert("You have gone bust click new game to play again!");
}
}
function keepcards()
{
if (total > randomnumber) {
alert("You win!");
}
else if (total < randomnumber) {
alert("You lose!");
}
else if (total === randomnumber) {
alert("It is a draw!");}
}
now = new Date();
localtime = now.toString();
hours = now.getHours();
mins = now.getMinutes();
secs = now.getSeconds();
document.write("<h1>");
document.write(hours + ":" + mins + ":" + secs);
document.write("</h1>");
<head>
<h1><i>BLACKJACK</i></h1>
<h4>
Computers Cards: <input type="text" id="computerscards" value="18">
<br>Player 1 cards: <input type="text" id="playerscards">
</h4>
</head>
<input type="button" value="start"
onclick="document.getElementById('playerscards').value = random()">
<input type="button" value="deal" onclick="dealcard()">
<input type="button" value="stand" onclick="keepcards()">
<input type="button" value="new game" onclick="window.location.reload()">
<p>To start the game press start and draw a card<br> Press deal to add a new
card <br> press stand if you do not wish to draw a new card<br> Press new game
if you want to refresh the page to play a mew game</p>
【问题讨论】:
-
line9: ` var total = randomnumber;` 之后我想你点击:
deal什么增加了total的值:total += Math.floor(Math.random() * 10 + 1);inkeepcards()你有这个:` if (total > randomnumber)`什么是真的。
标签: javascript html if-statement blackjack