JavaScript(ROCK,PAPER,SCISSORS)会在提示

时间:2018-03-11 16:54:21

标签: javascript loops

有人可以请我帮忙,它是我第三天学习JS而我试图添加do while循环直到摇滚,纸张或剪刀进入提示但似乎它不起作用,试图找出它现在几个小时......有了这个代码提示总是出现无关紧要。

// a. User makes a choice
var userChoice = prompt("Do you choose ROCK, PAPER or SCISSORS?");
do {
  userChoice = prompt("Do you choose ROCK, PAPER or SCISSORS?");
}
while (userChoice != "rock" && userChoice != "paper" && "scissors");
var computerChoice = Math.random();

// b. Computer makes a choice
if (computerChoice <= 0.33) {
  computerChoice = "rock";
} else if (computerChoice <= 0.67) {
  computerChoice = "paper";
} else {
  computerChoice = "scissors";
}
var choice1 = userChoice;
var choice2 = computerChoice;

// c. A compare function will determine who wins
function compare (choice1, choice2){
  if (choice1 === choice2){
    alert("A tie!");
  }
else if (choice1 === "rock"){
  if (choice2 === "paper"){
    alert("Computer chose PAPER, you LOSE!");
  } else
      alert("Computer chose SCISSORS, you WIN!");
  }
else if (choice1 === "paper"){
  if (choice2 === "scissors"){
    alert("Computer chose SCISSORS, you LOSE!");
  } else
      alert("Computer chose ROCK, you WIN!");
  }
else if (choice1 === "scissors"){
  if (choice2 === "rock"){
    alert ("Computer chose ROCK, you lose!");
  } else
      alert("Computer chose PAPER, you WIN!");
  }
}
compare (userChoice, computerChoice);
console.log("You chose:", choice1, "||", "Computer chose:", choice2);

2 个答案:

答案 0 :(得分:0)

始终显示

提示,因为

pod install

始终评估为true。你可能想要

import TCPickerView

答案 1 :(得分:0)

您的代码中存在以下几个问题:

  1. userChoice条件<{1}}内的and比较中缺少变量名称while
  2. 当代码运行(在第一行)和do块内时,您不需要提示用户选择。您只需在do块中提示用户选择。
  3. var userChoice;
    do {
      userChoice = prompt("Do you choose ROCK, PAPER or SCISSORS?");
    }
    while (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors");
    var computerChoice = Math.random();
    
    // b. Computer makes a choice
    if (computerChoice <= 0.33) {
      computerChoice = "rock";
    } else if (computerChoice <= 0.67) {
      computerChoice = "paper";
    } else {
      computerChoice = "scissors";
    }
    var choice1 = userChoice;
    var choice2 = computerChoice;
    
    // c. A compare function will determine who wins
    function compare (choice1, choice2){
      if (choice1 === choice2){
        alert("A tie!");
      }
    else if (choice1 === "rock"){
      if (choice2 === "paper"){
        alert("Computer chose PAPER, you LOSE!");
      } else
          alert("Computer chose SCISSORS, you WIN!");
      }
    else if (choice1 === "paper"){
      if (choice2 === "scissors"){
        alert("Computer chose SCISSORS, you LOSE!");
      } else
          alert("Computer chose ROCK, you WIN!");
      }
    else if (choice1 === "scissors"){
      if (choice2 === "rock"){
        alert ("Computer chose ROCK, you lose!");
      } else
          alert("Computer chose PAPER, you WIN!");
      }
    }
    compare (userChoice, computerChoice);
    console.log("You chose:", choice1, "||", "Computer chose:", choice2);