if语句仅应用第一个条件

时间:2017-12-21 18:15:11

标签: javascript

我正在创建一个简单的二十一点游戏,但是我在最后一步遇到了一个问题,我创建了一个if语句来决定用户支付胜利是否吸引,它似乎只执行声明中的第一个条件当按下支架按钮时,无论用户是赢还是输,它都会显示"you win"

<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="style.css">
<link href='http://fonts.googleapis.com/css?family=Merienda+One' rel='stylesheet' type='text/css'>

<title>
  Blackjack
</title>

<body>

  <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>
</body>
<script src="java.js"></script>

</html>

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>");

0 个答案:

没有答案
相关问题