gpt4 book ai didi

javascript - 在掷骰子游戏中输出得分结果的问题

转载 作者:行者123 更新时间:2023-12-01 01:12:12 25 4
gpt4 key购买 nike

我正在开发一个有点简单的骰子游戏,但当我单击按钮时,我无法让它显示警报弹出窗口。

我还试图找出最简单的方法来检查 housecore 或 myscore = maxscore 然后提醒“恭喜,你赢了”或“抱歉,你输了”

这是我的代码:

var myscore = 0;
var maxScore = 100;
var housescore = 0;

function rollDice() {
var x = Math.floor(Math.random() * 6) + 1;
var y = Math.floor(Math.random() * 6) + 1;

if (x + y == 7 || 11) {
housescore = (housescore + 10);
alert("CRAPS" + " Your Score is " + "Player: " + myscore + "House: " + housescore);
} else if (x == y && x + y == isEven(n)) {
myscore = (myscore + 10);
alert("Even Up" + " Your Score is " + "Player: " + myscore + "House: " + housescore);
}
if (x == y && x + y == isOdd(n)) {
housescore = (housescore + 10);
alert("Odd Ball" + " Your Score is " + "Player: " + myscore + "House: " + housescore);
} else {
alert("You rolled a " + x + " and a " + y + " Your Score is " + "Player: " + myscore + "House: " + housescore);
}
}

function isEven(n) {
return n % 2 == 0;
}

function isOdd(n) {
return Math.abs(n % 2) == 1;
}
<input type="button" value="Roll The Dice" onClick="rollDice()" />

最佳答案

我想这就是你所追求的。我分解了你的 ifs 来处理一个分支中 x == y 的两种情况,并使用一个简单的 mod 操作来确定骰子是否为偶数,并使用 else 来处理奇数骰子。

var myscore = 0;
var maxScore = 100;
var housescore = 0;

function rollDice() {
var x = Math.floor(Math.random() * 6) + 1;
var y = Math.floor(Math.random() * 6) + 1;

var total = x + y;
msg = "You rolled " + x + "," + y + " = " + total + "\n";

if (total == 7 || total == 11) {
housescore += 10;
msg += "CRAPS";
} else if (x == y) { // this condition contains two possible outcomes, handle both within this else if
if (x % 2 == 0) {
myscore += 10;
msg += "Even Up";
} else {
housescore += 10;
msg += "Odd Ball";
}
}
msg += " Your Score is " + "Player: " + myscore + ", House: " + housescore;
alert(msg);
}
<!DOCTYPE html>
<html>

<body>
<input type="button" value="Roll The Dice" onClick="rollDice()" />
</body>

</html>

关于javascript - 在掷骰子游戏中输出得分结果的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55068524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com