我正在制作一个基本的二十一点游戏,但我遇到了一些问题。我需要帮助使我的立场按钮执行名为keepcards的功能我不确定为什么它不工作,当按下支架按钮时不做出决定如果球员得分更高,则更少的矿石等于计算机得分。
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 > 21) {
alert("You lose!");
} else if (total > computerscards) {
alert("You win!");
} else if (total = computerscards) {
alert("It is a draw!");
} else if (total < computerscards) {
alert("You lose!");
} else {
alert("You lose!");
}
}
<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> Prsee new game if you want to refresh the page to play a mew game</p>
答案 0 :(得分:0)
else if (total = computerscards)
应为else if (total == computerscards)
您目前正在将计算机卡分配给总计,而不是将它们进行比较,结果总是如此,因此您永远不会点击下一个else if
。
编辑:
onclick="keepcards"
应为onclick="keepcards()"
此外,return randomnumber
将始终返回相同的号码,而不是每次都重新生成一个。您的函数应该return Math.floor(Math.random() * 10 + 1)
。
您的<head>
位于<body>
。
最后一次比较else if (total < computerscards)
没用,因为它与最终的else
做同样的事情。
答案 1 :(得分:0)
当提供函数作为字符串时,您需要包括括号()
。它应该是...onclick="keepcards()">
以下更新的代码:
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 > 21) {
alert("You lose!");
} else if (total > computerscards) {
alert("You win!");
} else if (total = computerscards) {
alert("It is a draw!");
} else if (total < computerscards) {
alert("You lose!");
} else {
alert("You lose!");
}
}
<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> Prsee new game if you want to refresh the page to play a mew game</p>
答案 2 :(得分:0)
查看<div class="things">
<div class="thing">
<h2>{{ thing.title }}</h2>
<p>{{ thing.content }}</p>
<small>{{ thing.date }}</small>
</div>
<div class="thing">
<h2>{{ thing.title }}</h2>
<p>{{ thing.content }}</p>
<small>{{ thing.date }}</small>
</div>
<div class="thing">
<h2>{{ thing.title }}</h2>
<p>{{ thing.content }}</p>
<small>{{ thing.date }}</small>
</div>
<div class="thing">
<h2>{{ thing.title }}</h2>
<p>{{ thing.content }}</p>
<small>{{ thing.date }}</small>
</div>
<div class="thing">
<h2>{{ thing.title }}</h2>
<p>{{ thing.content }}</p>
<small>{{ thing.date }}</small>
</div>
</div>
属性中所有函数调用结尾处的内容。看到缺少的东西?
onclick
vs ThisIsAFunction()
答案 3 :(得分:0)
您没有在onclick
事件中调用您的函数,因此永远不会调用它。试试<input type="button" value="stand" onclick="keepcards()">