gpt4 book ai didi

JavaScript 给出错误的输出

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

我正在创建游戏(布/剪刀/石头),在最后一部分中,我想比较 userChoice 与 ComputerChoice,但由于某种原因,我得到了错误的输出:当我按下按钮(例如“石头”和计算机)时选择“剪刀”我得到以下输出:“你选择了石头”。电脑选剪刀你就输了!再试一次。'但这是错误的!应该在另一边。当“这是平局!”时,我也没有得到返回;你能帮忙吗?

//user choice
var output = document.getElementById("output");
var result = document.getElementById("result");
var count = 3;
var countUser = 0;
var countComputer = 0;
var paper = document.querySelector("#paper header");
paper.addEventListener("click", function() {
paper.classList.toggle("header-special");
userChoice = "paper";
output.innerHTML = "You Chose Paper";
compareWithComputer("paper");
});

var scissors = document.querySelector("#scissors header");
scissors.addEventListener("click", function() {
scissors.classList.toggle("header-special");
userChoice = "scissors";
output.innerHTML = "You Chose Scissors";
compareWithComputer("scissors");
});

var stone = document.querySelector("#stone header");
stone.addEventListener("click", function() {
stone.classList.toggle("header-special");
userChoice = "stone";
output.innerHTML = "You Chose Stone";
compareWithComputer("stone");
});

// Computer choice

function compareWithComputer(userChoice) {
var computerChoice = Math.floor(Math.random() * 3 + 1);
if (computerChoice == 1) {
computerChoice = "Stone";
} else if (computerChoice == 2) {
computerChoice = "Paper";
} else {
computerChoice = "Scissors";
}

var results = compare(userChoice, computerChoice);

output.innerHTML +=
". Computer Chose " +
computerChoice +
results;
result.innerHTML = "user " + countUser + "computer" + countComputer;
}
// Compare userChoice and computerChoice
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "It's a tie!";
}
if (choice1 === "stone") {
if (choice2 === "scissors") {
// stone wins
countUser++;
return "You win!";

} else {
// paper wins
countComputer++;
return "You lose! Try again.";

}
}
if (choice1 === "paper") {
if (choice2 === "stone") {
// paper wins
countUser++;
return "You win!";


} else {
// scissors wins
countComputer++;
return "You lose! Try again.";

}
}
if (choice1 === "scissors") {
if (choice2 === "stone") {
// stone wins
countComputer++;
return "You lose! Try again.";

} else {
// scissors wins
countUser++;
return "You win!";

}
}
};
<!DOCTYPE html>
<div class="start" <h1>Click the button, start the game!</h1>
</div>
<div class="game" id="paper">
<header>Paper</header>

</div>
<div class="game" id="scissors">
<header>Scissors</header>
</div>
<div class="game" id="stone">
<header>Stone</header>
</div>
<div id="output"></div>
<div id="result" </div>

最佳答案

在函数 compareWithComputer 中,您返回以大写字母开头的字符串。这些不等于小写字母的对应项 ('Stone' !== 'stone')。

此代码有效:

    //user choice
var output =document.getElementById("output");
var result =document.getElementById("result");
var count=3;
var countUser=0;
var countComputer=0;
var paper = document.querySelector("#paper header");
paper.addEventListener("click", function() {
paper.classList.toggle("header-special");
userChoice = "paper";
output.innerHTML = "You Chose Paper";
compareWithComputer("paper");
});

var scissors = document.querySelector("#scissors header");
scissors.addEventListener("click", function() {
scissors.classList.toggle("header-special");
userChoice = "scissors";
output.innerHTML = "You Chose Scissors";
compareWithComputer("scissors");
});

var stone = document.querySelector("#stone header");
stone.addEventListener("click", function() {
stone.classList.toggle("header-special");
userChoice = "stone";
output.innerHTML = "You Chose Stone";
compareWithComputer("stone");
});

// Computer choice

function compareWithComputer(userChoice) {
var computerChoice = Math.floor(Math.random() * 3 + 1);
if (computerChoice == 1) {
computerChoice = "stone";
} else if (computerChoice == 2) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}

var results = compare(userChoice, computerChoice);

output.innerHTML +=
". Computer Chose " +
computerChoice +
results;
result.innerHTML="user "+countUser+"computer"+countComputer;
}
// Compare userChoice and computerChoice
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "It's a tie!";
}
if (choice1 === "stone") {
if (choice2 === "scissors") {
// stone wins
countUser++;
return "You win!";

} else {
// paper wins
countComputer++;
return "You lose! Try again.";

}
}
if (choice1 === "paper") {
if (choice2 === "stone") {
// paper wins
countUser++;
return "You win!";


} else {
// scissors wins
countComputer++;
return "You lose! Try again.";

}
}
if (choice1 === "scissors") {
if (choice2 === "stone") {
// stone wins
countComputer++;
return "You lose! Try again.";

} else {
// scissors wins
countUser++;
return "You win!";

}
}
};
    <!DOCTYPE html>
<div class ="start"
<h1>Click the button, start the game!</h1>
</div>
<div class="game" id="paper">
<header>Paper</header>

</div>
<div class="game" id="scissors">
<header>Scissors</header>
</div>
<div class="game" id="stone">
<header>Stone</header>
</div>
<div id="output"></div>
<div id="result"</div>

关于JavaScript 给出错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55730656/

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