我是新手,我不能为我的生活找到这种语法错误

时间:2015-07-17 03:19:25

标签: javascript

我不明白为什么第12行出现语法错误,但似乎有一个。

g = 0
y = 0
c = 0
while(true) {
computer = random(3);

you = choose("Choose!", "Rock", "Paper", "Scissors");

if (computer == 1){
  if (you == 2){
      show("You win!"); 
      g = g + 1;
      y = y + 1; 
 if (g == 3) {
     break; 
     show("Computer: " + c + "You: " + y);
 }
} else if (you == 1) {
    show("Draw"); 
   g = g + 1; 
   if (g == 3) {
   break;
    show("Computer: " + c + "You: " + y;)
  }
} else if (you == 3) {
  show("Computer wins."); 
  g = g + 1; 
 c = c + 1; 
    if (g == 3) {
     break;
      show("Computer: " + c + "You: " + y;)
}
}

}

4 个答案:

答案 0 :(得分:6)

y;)}应该在y);}两个地方

另一个问题,你在三个地方都有break; show(...show我不认为会在break之后运行

var g = 0,
    y = 0,
    c = 0;

while (true) {
    computer = random(3);

    you = choose("Choose!", "Rock", "Paper", "Scissors");

    if (computer == 1) {
        if (you == 2) {
            show("You win!");
            g = g + 1;
            y = y + 1;
            if (g == 3) {
                break; // <=== break here?
                show("Computer: " + c + "You: " + y);
            }
        } else if (you == 1) {
            show("Draw");
            g = g + 1;
            if (g == 3) {
                break; // <=== break here?
                show("Computer: " + c + "You: " + y;) // <=== don't wink here ;)
            }
        } else if (you == 3) {
            show("Computer wins.");
            g = g + 1;
            c = c + 1;
            if (g == 3) {
                break; // <==== break here?
                show("Computer: " + c + "You: " + y;) // <=== don't wink here ;)
            }
        }
    }
}

P.S。在发布

之前,我使用http://jsbeautifier.org/来美化我的代码

答案 1 :(得分:2)

更容易看出你是否真的清理了代码。

g = 0
y = 0
c = 0
while (true) {
    computer = random(3);

    you = choose("Choose!", "Rock", "Paper", "Scissors");

    if (computer == 1) {
        if (you == 2) {
            show("You win!");
            g = g + 1;
            y = y + 1;
            if (g == 3) {
                break;
                show("Computer: " + c + "You: " + y);
            }
        } else if (you == 1) {
            show("Draw");
            g = g + 1;
            if (g == 3) {
                break;
                show("Computer: " + c + "You: " + y;)
            }
        } else if (you == 3) {
            show("Computer wins.");
            g = g + 1;
            c = c + 1;
            if (g == 3) {
                break;
                show("Computer: " + c + "You: " + y;)
            }
        }
    }

你有几个问题

  1. show("Computer: " + c + "You: " + y;)应为show("Computer: " + c + "You: " + y);
  2. 未公开的while循环。
  3. 未定义的功能,例如randomchooseshow
  4. 在实际运行代码之前
  5. break。 (虽然不会导致任何错误,但很可能并非如此)

答案 2 :(得分:0)

你最后错过了'}'。尝试beautify js缩进代码并使用jsHint发现错误。

答案 3 :(得分:-2)

可能是因为你在“休息”之后仍然有陈述;这是无法到​​达的。