gpt4 book ai didi

JavaScript 函数 if 语句

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

我正在用 javascript 制作一个骰子游戏,你可以打赌结果是 ODD 还是 EVEN ,当你按下按钮时,骰子就会滚动,并显示它是奇数还是偶数。我唯一的问题是,当你投注偶数而得到奇数时,它不会将其记录为损失,或者如果你投注偶数并得到偶数,则不会记录为胜利。总而言之,我不知道为什么它不记录玩家的分数。感谢所有帮助,谢谢,代码也在此处。

var score = 0;
var lose = 0;
document.getElementById("Even").addEventListener("click", Evencheck);

function Evencheck() {
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var d11 = parseInt(d1);
var d22 = parseInt(d2);
var d33 = parseInt(d11 + d22);
die1.innerHTML = "Dice 1 rolled: " + d1;
die2.innerHTML = "Dice 2 rolled: " + d2;
sum.innerHTML = " Total Sum: " + d33;
if (d33 % 2 == 0 && Even.Clicked) {
var score = score + 1;
wins.innerHTML = " Wins : " + score;
losses.innerHTMl = " Losses : " + lose;
}
if (d33 % 2 == 1 && Even.Clicked) {
var lose = lose + 1;
wins.innerHTML = " Wins : " + score;
losses.innerHTMl = " Losses : " + lose;
}
}
document.getElementById("Odd").addEventListener("click", Oddcheck);

function Oddcheck() {
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var d11 = parseInt(d1);
var d22 = parseInt(d2);
var d33 = parseInt(d11 + d22);
die1.innerHTML = "Dice 1 rolled: " + d1;
die2.innerHTML = "Dice 2 rolled: " + d2;
sum.innerHTML = " Total Sum: " + d33;
if (d33 % 2 == 0 && Odd.Clicked) {
var lose = lose + 1;
wins.innerHTML = " Wins : " + score;
losses.innerHTMl = " Losses : " + lose;
}
if (d33 % 2 == 1 && Odd.Clicked) {
var score = score + 1;
wins.innerHTML = " Wins : " + score;
losses.innerHTMl = " Losses : " + lose;
}
}
<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" href="defaults.css">
<title>INTRO</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
<h1><img src="dice.png" alt="dice" width="50" height="50"> EVEN ODDS<img src="dice.png" alt="dice" width="50" height="50"> </h1>
<div id="die1" class="dice"> Dice1 :0</div>
<div id="die2" class="dice"> Dice2 :0</div>
<div id="sum" class="dicesum"> Sum :0</div>
<div id="wins" class="score"> Wins :0</div>
<div id="losses" class="lose"> Losses :0</div>
<button class="Even" onclick="Evencheck()">Even</button>
<button class="Odd" onclick="Oddcheck()">ODD</button>

</body>

</html>

最佳答案

你遇到了一些小逻辑错误,我建议你利用 JavaScript,你可以只使用一个“main”模块(我们称之为 play()) 您可以在其中处理游戏逻辑。

此外,如果您不使用定义的 DOM 引用,最好删除它们。代码越少越好。 (大多数时候)

查看以下更正:

发生了什么变化?:

  • 添加了一个“log” div,以便玩家可以看到正在发生的事情。
  • 删除了未使用的 DOM 引用。
  • 添加了 play() 函数,用于处理游戏的所有内容。
  • 添加了 rollDice() 函数,可以掷出您想要的骰子数量。

就是这样,更少的代码,更多的人类可读性,并且可以工作。测试一下:

let won = 0;
let lost = 0;

function rollDice(amount=1) { // A module that rolls the amount dices you want
let dices = [];
for(let c=0;c<amount;c++) {
dices.push(+Math.floor(Math.random() * 6) + 1);
}
return dices; // Returns it as an array
}

function play(selection) {
let dices = rollDice(2); // Roll two dices
let sum = dices[0] + dices[1];
die1.innerHTML = "Dice 1 rolled: " + dices[0];
die2.innerHTML = "Dice 2 rolled: " + dices[1];
res.innerHTML = " Total Sum: " + sum;
let result = sum % 2 === 0 ? "even" : "odd"; // Ternary if statement to get the result
log.innerHTML = `
You selected ${selection} and result is ${result}.
You ${selection===result ? "won" : "lost"}.
`;
if(selection===result) { // If statement that handles score based on result
won++;
} else {
lost++;
}
wins.innerHTML = "Wins: " + won; // Update both results on screen after played
losses.innerHTML = "Losses: " + lost;
}
<img src="dice.png" alt="dice" width="50" height="50">
<h1>EVEN</h1>
<h1>ODDS</h1>
<img src="dice.png" alt="dice" width="50" height="50">
<div id="die1" class="dice"> Dice1 :0</div>
<div id="die2" class="dice"> Dice2 :0</div>
<div id="res" class="dicesum"> Sum :0</div>
<div id="wins" class="score"> Wins :0</div>
<div id="losses" class="lose"> Losses :0</div>
<button class="even" name="even" onclick="play(this.name)">Even</button>
<button class="odd" name="odd" onclick="play(this.name)">ODD</button>
<div id="log"></div>

关于JavaScript 函数 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58326561/

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