我已经设置了两个单独的游戏"两个骰子随机出现,并计算两者的得分。我无法做的是展示"胜利者"而且"输家"。因此,玩家1和玩家2,以及我想要做的是在得分最高或最低的玩家之下写下赢家或输家。我尝试使用javascript自己计算它但没有找到的代码是正确的。我在网上到处搜索但似乎没有任何工作,所以我不知道我做错了也很抱歉我这么久我非常需要,因为我所做的一切都不起作用,我在这里的代码只是骰子和分数,但没有"胜利者"或者"松散"
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<script>
var randomNumber1 = Math.floor (Math.random()*6)+1;
var randomNumber2 = Math.floor (Math.random()*6)+1;
var opentag = " <img src = 'die";
var closetag = ".gif'>";
var imageTagString1 = opentag + randomNumber1 + closetag;
var imageTagString2 = Math.floor(Math.random()*6)+1;
var imageTagString2 = " <img src = \"die" + randomNumber2 + ".gif\">";
var score = randomNumber1 + randomNumber2;
document.write ("<p>The Score is : " + score);
document.write(imageTagString1);
document.write(imageTagString2);
</script>
</div>
<div>
<script>
var randomNumberA = Math.floor (Math.random()*6)+1;
var randomNumberB = Math.floor (Math.random()*6)+1;
var opentag = " <img src = 'die";
var closetag = ".gif'>";
var imageTagString1 = opentag + randomNumberA + closetag;
var imageTagString2 = Math.floor(Math.random()*6)+1;
var imageTagString2 = " <img src = \"die" + randomNumberB + ".gif\">";
var score2 = randomNumberA + randomNumberB;
document.write ("<p>The Score is : " + score);
document.write(imageTagString1);
document.write(imageTagString2);
</script>
</div>
</body>
</html>
答案 0 :(得分:0)
假设你在与html文件相同的目录中有die1.gif图像die1.gif,下面的代码应该没问题:
<!DOCTYPE html>
<html>
<body>
<script type ='text/javascript'>
var player1Dice1 = Math.floor (Math.random()*6)+1;
var player1Dice2 = Math.floor (Math.random()*6)+1;
var player2Dice1 = Math.floor (Math.random()*6)+1;
var player2Dice2 = Math.floor (Math.random()*6)+1;
var scoreP1 = player1Dice1 + player1Dice2;
var scoreP2 = player2Dice1 + player2Dice2;
var opentag = " <img src = 'die";
var closetag = ".gif'>";
var p1d1 = opentag + player1Dice1 + closetag;
var p1d2 = opentag + player1Dice2 + closetag;
var p2d1 = opentag + player2Dice1 + closetag;
var p2d2 = opentag + player2Dice2 + closetag;
document.write ("<p>Player 1 score is : " + scoreP1);
document.write(p1d1);
document.write(p1d2);
document.write ("<p>Player 2 score is : " + scoreP2);
document.write(p2d1);
document.write(p2d2);
if (scoreP1 > scoreP2) {
document.write("<p>Player 1 wins. Player 2 loses.</p>");
}
else if (scoreP2 > scoreP1) {
document.write("<p>Player 2 wins. Player 1 loses.</p>");
}
else {
document.write("<p>Draw.</p>");
}
</script>
</body>
</html>
另请阅读this。