2017-09-26 72 views
0

每次我運行這個程序時,即使我把石頭或紙張放入提示中,也會彈出一個提示「Scissors vs. x」。如果我放入紙中,應該說「搖擺與x」,如果我把紙放在「紙與x」中,但由於某種原因,它總是會去剪刀,除非我放入的值是相同的值計算機生成。爲什麼我的搖滾紙剪刀程序不工作?

function setup() { 

    alert("Welcome to Rock Paper Scissors!"); 
} 

function draw() { 

var opponentChoice = Math.random(); //Setup for game 
var yourChoice = prompt("Rock, paper or scissors?"); 
if (opponentChoice <0.34){ 
    opponentChoice = "rock"; 
}else if(opponentChoice <=0.67){ 
    opponentChoice = "paper"; 
} 
else{ 
    opponentChoice = "scissors"; 
} //End of setup for game 

function compare (firstOption,secondOption){ //Game logic 
    if(firstOption===secondOption){ //Tie if both are the same 
     alert(firstOption + " vs. " + secondOption); 
     alert("It was a tie!"); 
    } 
    else if(firstOption==="rock"){ //If user types rock 
     alert("Rock vs. " + secondOption); 
     if(secondOption==="scissors"){ //If computer generates scissors 
      alert("Rock wins"); 
     } 
     else { //If computer generates paper 
      alert("Paper wins"); 
     } 
    } 
    else if(firstOption==="scissors"){ //If user types scissors 
     alert("Scissors vs. " + secondOption); 
     if(secondOption==="rock"){ //If computer generates rock 
      alert("Rock wins!"); 
     } 
     else { //If computer generates paper 
      alert("Scissors wins"); 
     } 
    } 
    else if(firstOption==="paper"){ //If user types paper 
     alert("Paper vs. " + secondOption); 
     if(secondOption==="rock"){ //If computer generates rock 
      alert("Paper wins"); 
     } 
     else { //If computer generates scissors 
      alert("Scissors wins"); 
     } 
    } 
    else{ 
     alert("Please choose one of the options in lower case"); 
    } 
}; 
compare(yourChoice,opponentChoice); //Function is called here 

} 
+0

你能提供給我用,你發現了錯誤示例的完整的代碼? – zhuravlyov

回答

0

"use strict"; 
 

 
function draw() { 
 

 
    let opponentChoice = Math.random(); //Setup for game 
 
    let yourChoice = prompt("Rock, paper or scissors?"); 
 
    if (opponentChoice <0.34){ 
 
     opponentChoice = "rock"; 
 
    }else if(opponentChoice <=0.67){ 
 
     opponentChoice = "paper"; 
 
    } 
 
    else{ 
 
     opponentChoice = "scissors"; 
 
    } //End of setup for game 
 

 
    function compare (firstOption,secondOption){ //Game logic 
 
     if(firstOption===secondOption){ //Tie if both are the same 
 
      alert(firstOption + " vs. " + secondOption); 
 
      alert("It was a tie!"); 
 
     } 
 
     else if(firstOption==="rock"){ //If user types rock 
 
      alert("Rock vs. " + secondOption); 
 
      if(secondOption==="scissors"){ //If computer generates scissors 
 
       alert("Rock wins"); 
 
      } 
 
      else { //If computer generates paper 
 
       alert("Paper wins"); 
 
      } 
 
     } 
 
     else if(firstOption==="scissors"){ //If user types scissors 
 
      alert("Scissors vs. " + secondOption); 
 
      if(secondOption==="rock"){ //If computer generates rock 
 
       alert("Rock wins!"); 
 
      } 
 
      else { //If computer generates paper 
 
       alert("Scissors wins"); 
 
      } 
 
     } 
 
     else if(firstOption==="paper"){ //If user types paper 
 
      alert("Paper vs. " + secondOption); 
 
      if(secondOption==="rock"){ //If computer generates rock 
 
       alert("Paper wins"); 
 
      } 
 
      else { //If computer generates scissors 
 
       alert("Scissors wins"); 
 
      } 
 
     } 
 
     else{ 
 
      alert("Please choose one of the options in lower case"); 
 
     } 
 
    } 
 

 
    compare(yourChoice,opponentChoice); //Function is called here 
 

 
} 
 

 
draw();

看起來像它工作正常