首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >第7/9课错误

第7/9课错误
EN

Stack Overflow用户
提问于 2015-09-24 05:32:36
回答 5查看 1.3K关注 0票数 0

我已经被困在7/9天两个晚上了。这是一个摇滚剪刀游戏。我不知道是怎么回事。我试了一下在线衣领,它还说我的第22行是一个错误(期望是一个标识符,而不是看到“tried”)。按照说明,如果在比较函数中的现有代码中,我还编写了另一个代码。

我的代码:

代码语言:javascript
复制
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";
}
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2015-09-24 05:46:42

我在您的代码中看到了几个语法错误。它应该如下所示:

代码语言:javascript
复制
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";
    }
  }
}
票数 0
EN

Stack Overflow用户

发布于 2015-09-24 05:36:55

代码语言:javascript
复制
 else if(choice1 ==="paper");{
    if(choice2 ==="rock")
    return("paper wins");
 }

你要终止你是else if;的条件下

它应该是:

代码语言:javascript
复制
else if(choice1 ==="paper"){
        if(choice2 ==="rock")
        return("paper wins");
}
票数 1
EN

Stack Overflow用户

发布于 2015-09-24 05:37:06

代码语言:javascript
复制
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";
}
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32754026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档