你的if else语句在我的石头剪刀布游戏中有什么问题?

huangapple go评论92阅读模式
英文:

What is wrong with my if else statements for my rock paper scissor game?

问题

I have translated the code for you. Here it is:

  1. function getComputerChoice() {
  2. const randomNumber = Math.floor(Math.random()*3);
  3. switch(randomNumber) {
  4. case 0:
  5. return "rock";
  6. case 1:
  7. return "paper";
  8. case 2:
  9. return "scissors";
  10. };
  11. }
  12. function getPlayerChoice() {
  13. return prompt("rock, paper, or scissors?").toLowerCase();
  14. }
  15. function playRound(playerSelection, computerSelection) {
  16. computerSelection = getComputerChoice();
  17. playerSelection = getPlayerChoice();
  18. if (computerSelection === playerSelection) {
  19. console.log("Tie");
  20. } else if (computerSelection === "rock" && playerSelection === "scissors") {
  21. console.log("You lose! Rock beats Scissors");
  22. } else if (computerSelection === "paper" && playerSelection === "rock") {
  23. console.log("You lose! Paper beats Rock");
  24. } else if (computerSelection === "scissors" && playerSelection === "paper") {
  25. console.log("You lose! Scissors beats Paper");
  26. } else if (playerSelection === "rock" && computerSelection === "scissors") {
  27. console.log("You win! Rock beats Scissors");
  28. } else if (playerSelection === "paper" && computerSelection === "rock") {
  29. console.log("You win! Paper beats Rock");
  30. } else if (playerSelection === "scissors" && computerSelection === "paper") {
  31. console.log("You win! Scissors beats Paper");
  32. }
  33. }

I've removed the HTML entities (e.g., ") and provided a cleaner version of your JavaScript code.

英文:

I am attempting to make a Rock Paper Scissors game in JavaScript, however, every time I run it the console logs the last option "You win! Scissors beats Paper" even if my selection was something other than scissors. I believe I made an error with my if else statements but maybe the problem comes from some other part.

  1. function getComputerChoice() {
  2. const randomNumber = Math.floor(Math.random()*3);
  3. switch(randomNumber) {
  4. case 0:
  5. return "rock";
  6. case 1:
  7. return "paper";
  8. case 2:
  9. return "scissors";
  10. };
  11. }
  12. function getPlayerChoice() {
  13. prompt("rock paper or scissors?").toLowerCase();
  14. }
  15. function playRound (playerSelection,computerSelection) {
  16. computerSelection = getComputerChoice()
  17. playerSelection = getPlayerChoice()
  18. if (computerSelection === playerSelection) {
  19. console.log ("Tie");
  20. }
  21. else if (computerSelection === "rock" && playerSelection === "scissors") {
  22. console.log ("You lose! Rock beats Scissors");
  23. }
  24. else if (computerSelection === "paper" && playerSelection === "rock") {
  25. console.log ("You lose! Paper beats Rock");
  26. }
  27. else if (computerSelection === "scissors" && playerSelection === "paper") {
  28. console.log ("You lose! Scissors beats Paper");
  29. }
  30. else if (playerSelection === "rock" && computerSelection === "scissors") {
  31. console.log ("You win! Rock beats Scissors");
  32. }
  33. else if (playerSelection === "paper" && computerSelection === "rock") {
  34. console.log ("You win! Paper beats Rock");
  35. }
  36. else (playerSelection === "scissors" && computerSelection === "paper") ;{
  37. console.log ("You win! Scissors beats Paper");
  38. }
  39. }

答案1

得分: 1

有两个问题导致代码无法正常运行。首先,你的函数 getPlayerChoice() 没有返回任何值。结果,playerSelection === undefined 而不是你预期的值。由于你的 if 语句没有考虑到这一点,它们最终都变为 false。这导致了执行你的 else 语句。

应该是:

  1. function getPlayerChoice() {
  2. return prompt("rock paper or scissors?").toLowerCase();
  3. }

第二个问题是 else 语句内部不应该有条件。

解决方案:

  1. else {
  2. console.log("You win! Scissors beats Paper");
  3. }

请注意,在你的第一个函数的 switch 语句后面不需要分号,但似乎它并不导致代码故障。

英文:

There are two issues that makes the code not run as it should. The first is that your function getPlayerChoice() does not return any values. As a result, playerSelection === undefined instead of your intended values. Since your if statements don't account for that, they all end up false. This leads to your else statement being executed.

It should be:

  1. function getPlayerChoice() {
  2. return prompt("rock paper or scissors?").toLowerCase();
  3. }

The second is that else should not have a condition inside it.

Solution:

  1. else {
  2. console.log ("You win! Scissors beats Paper");
  3. }

Note that you don't need the semicolon after your switch statement in your first function, but it doesn't seem to cause the code to malfunction.

huangapple
  • 本文由 发表于 2023年5月22日 09:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定