我已经被困在7/9天两个晚上了。这是一个摇滚剪刀游戏。我不知道是怎么回事。我试了一下在线衣领,它还说我的第22行是一个错误(期望是一个标识符,而不是看到“tried”)。按照说明,如果在比较函数中的现有代码中,我还编写了另一个代码。
我的代码:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice ="rock"
} else if(computerChoice <= 0.67) {
computerChoice ="paper";
} else {
computerChoice ="scissors";
} console.log("Computer: " + computerChoice);
var compare=function(choice1, choice2){
if(choice1 === choice2) {
return("The result is a tie!");
}
else if(choice1 ==="rock"){
if(choice2 ==="scissors")
return("rock wins");
}
else{
return"paper wins";
}
else if(choice1 ==="paper");{
if(choice2 ==="rock")
return("paper wins");
}
else{
return"scissors wins";
}
}发布于 2015-09-24 05:46:42
我在您的代码中看到了几个语法错误。它应该如下所示:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return("The result is a tie!");
} else if(choice1 === "rock") {
if(choice2 === "scissors") {
return("rock wins");
} else {
return "paper wins";
}
} else if(choice1 ==="paper") {
if(choice2 ==="rock") {
return("paper wins");
} else {
return"scissors wins";
}
}
}发布于 2015-09-24 05:36:55
else if(choice1 ==="paper");{
if(choice2 ==="rock")
return("paper wins");
}你要终止你是else if在;的条件下
它应该是:
else if(choice1 ==="paper"){
if(choice2 ==="rock")
return("paper wins");
}发布于 2015-09-24 05:37:06
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice ="rock"
} else if(computerChoice <= 0.67) {
computerChoice ="paper";
} else {
computerChoice ="scissors";
} console.log("Computer: " + computerChoice);
var compare=function(choice1, choice2){
if(choice1 === choice2) {
return("The result is a tie!");
}
else if(choice1 ==="rock"){
if(choice2 ==="scissors")
return("rock wins");
}
else{
return"paper wins";
}
else if(choice1 ==="paper");{ -- on this there is semicolon after elseif block.. and how come else if is there after else block..
if(choice2 ==="rock")
return("paper wins");
}
else{
return"scissors wins";
}
}https://stackoverflow.com/questions/32754026
复制相似问题