【发布时间】:2016-03-08 23:59:37
【问题描述】:
我正在努力将一对骰子的随机掷骰相加,以根据用户定义的 n 掷骰。代码如下:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="dice.css">
<script>
//supposed to give 2 random numbers
function roll() {
var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
var Total = x + y;
}
// ask for user to put how many times they want to roll the two dice
function myinput() {
var NumRoll = prompt("Please enter the number of times you wish to roll");
if (NumRoll <= 0 )
document.getElementById("wrong").innerHTML = "You entered an invalid number enter number between 1-100";
else if ( NumRoll >100 )
document.getElementById("wrong").innerHTML = "You enter an invalid number enter number between 1-100";
else
document.getElementById("right").innerHTML = "Rolling the dice " + NumRoll + " times";
}
</script>
</head>
<body>
<p id="wrong"> </p>
<p1 id="right"></p1>
<p>Click to roll dice</p>
<button onclick="myinput(); roll()">Press</button>
</body>
</html>
【问题讨论】:
-
所以用户要求掷骰子 23 次,骰子将掷 23 次,将所有掷出的数字加在一起我不知道如何将两者联系起来
标签: javascript random