需要帮助在我的石头布和剪刀游戏javascript
当用户和计算机捆绑在一起时,它总是告诉我,我输了,而不是被捆绑在一起需要帮助在我的石头布和剪刀游戏javascript,javascript,if-statement,Javascript,If Statement,当用户和计算机捆绑在一起时,它总是告诉我,我输了,而不是被捆绑在一起 <html> <head> <title>Rock, Paper and Scissors</title> </head> <body> <p>Rock, Paper and Scissors!</p> <input type="text" id ="te
<html>
<head>
<title>Rock, Paper and Scissors</title>
</head>
<body>
<p>Rock, Paper and Scissors!</p>
<input type="text" id ="textChoice">
<button id="submit">Submit</button>
<p id="paragraphChoice"></p>
<script type="text/javascript">
document.getElementById("submit").onclick = function() {
var computerChoice = ["rock", "paper", "scissors"];
var computerGuess =
computerChoice[Math.floor(Math.random() * computerChoice.length)];
var userGuess = document.getElementById("textChoice").value;
if (userGuess === computerGuess) {
document.getElementById("paragraphChoice").innerHTML =
"You tied! The computer choose "+ computerGuess; //Both tied!
}
if (userGuess === "rock") {
if (computerGuess === "scissors") {
//rock wins
document.getElementById("paragraphChoice").innerHTML =
"You won! The computer choose "+ computerGuess;
} else {
//paper wins
document.getElementById("paragraphChoice").innerHTML =
"You lost! The computer choose "+ computerGuess;
}
}
if (userGuess === "paper") {
if (computerGuess === "rock") {
//rock wins
document.getElementById("paragraphChoice").innerHTML =
"You won! The computer choose "+ computerGuess;
} else {
//scissors wins
document.getElementById("paragraphChoice").innerHTML =
"You lost! The computer choose "+ computerGuess;
}
}
if (userGuess === "scissors") {
if (computerGuess === "paper") {
document.getElementById("paragraphChoice").innerHTML =
"You won! The computer choose "+ computerGuess;
} else {
document.getElementById("paragraphChoice").innerHTML =
"You lost! The computer choose "+ computerGuess;
}
}
};
</script>
</body>
</html>
石头、布和剪刀
石头,布和剪刀
提交
document.getElementById(“提交”).onclick=function(){
var computerChoice=[“石头”、“布”、“剪刀”];
var computerGuess=
computerChoice[Math.floor(Math.random()*computerChoice.length)];
var userGuess=document.getElementById(“textChoice”).value;
如果(userGuess==computerGuess){
document.getElementById(“paragraphChoice”).innerHTML=
“你打成平局!计算机选择”+computerGuess;//两个都打成平局!
}
如果(userGuess==“rock”){
如果(计算机猜测==“剪刀”){
//摇滚乐获胜
document.getElementById(“paragraphChoice”).innerHTML=
“你赢了!电脑选择”+电脑猜;
}否则{
//报纸获胜
document.getElementById(“paragraphChoice”).innerHTML=
“你输了!电脑选择”+电脑猜;
}
}
如果(userGuess==“纸张”){
如果(计算机猜测==“岩石”){
//摇滚乐获胜
document.getElementById(“paragraphChoice”).innerHTML=
“你赢了!电脑选择”+电脑猜;
}否则{
//剪刀赢了
document.getElementById(“paragraphChoice”).innerHTML=
“你输了!电脑选择”+电脑猜;
}
}
如果(userGuess==“剪刀”){
如果(计算机猜测==“纸张”){
document.getElementById(“paragraphChoice”).innerHTML=
“你赢了!电脑选择”+电脑猜;
}否则{
document.getElementById(“paragraphChoice”).innerHTML=
“你输了!电脑选择”+电脑猜;
}
}
};
到目前为止,我还没有学习函数,if语句非常混乱(我就是这么想的)
非常感谢你的帮助 当你得到一个答案时,你就完成了:如果2==2,那么就完成了。不要检查其他可能性。所以
if (userGuess === computerGuess) {
document.getElementById("paragraphChoice").innerHTML =
"You tied! The computer choose "+ computerGuess; //Both tied!
// you are done. return.
return;
}
如果您不告诉程序您完成了,它将继续检查所有其他if-then
块,最后一个块将打印到页面。您必须在每个if-then
块中添加一个return