繁体   English   中英

JavaScript提供错误的输出

[英]JavaScript giving wrong output

我正在创建游戏(纸/剪刀/石头),在最后一节中,我想比较userChoice与computerChoice,但是由于某些原因,我得到了错误的输出:当我按下按钮时,例如“ Stone”,计算机选择“ Scissors” '我得到以下输出:'You选择石头。 电脑选择剪刀你输了! 再试一次。' 但这是错误的! 应该在另一边。 当“这是一条领带!”时,我也没有得到回报。 你能帮忙吗?

 //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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM