私は0-5 + 1(Dice。)の間の乱数を生成するJavascriptを使って簡単なスクリプトを作った。私の問題は、私がボタンを押したときに半分の時間、ダイスロールが現れる前に。私のボタンがスクリプトで動作しないのはなぜですか?
私はどこからでも見ましたが、問題の原因については何も見つかりませんでした。 Math.floor(Math.random() * 6) + 1;
は7
を返す可能性があるため
<html>
<head>
</head>
<body>
<button type="button" onclick="spin()">Click here to roll the Dice!</button>
<img src="" id="demo">
<script>
function spin() {
if (Dice() == 1) {
document.getElementById("demo").src="http://i.imgur.com/QRTs9Ax.png";
}
else if (Dice() == 2) {
document.getElementById("demo").src="http://i.imgur.com/OMz1o8U.png";
}
else if (Dice() == 3) {
document.getElementById("demo").src="http://i.imgur.com/J4Xx2yO.png";
}
else if (Dice() == 4) {
document.getElementById("demo").src="http://i.imgur.com/CJb2ojk.png";
}
else if (Dice() == 5) {
document.getElementById("demo").src="http://i.imgur.com/8W6UL5O.png";
}
else if (Dice() == 6) {
document.getElementById("demo").src="http://i.imgur.com/NGxBete.png";
}
}
</script>
<script>
function Dice() {
return Math.floor(Math.random() * 6) + 1;
}
</script>
</body>
</html>
私はそれが見、1〜6の間の値を返すことができポジティブだ、これは7を返すことができるとは思わない:http://stackoverflow.com/a/4960020/1333836 –