2014-10-31 119 views
-2

好吧,这里是我的代码,任何人都知道如何解决这个问题?它甚至不会运行。Javascript语法错误:失踪)参数列表后:上列61

<!DOCTYPE html> 

<html> 
<head> 
<title>Hiding Game</title> 
<meta charset="UTF-8" /> 
<link rel="stylesheet" type="text/css" href="proj3.css" /> 
</head> 
<body> 

<script type="text/javascript"> 
    var counter = 0; 
     document.writeln("<table>") 
     document.writeln("<th>Day</th>") 
     document.writeln("<th>Hiding Place</th>") 
     document.writeln("<th>Explosion</th>") 
    do{ 
     counter = counter + 1 
     var hide = Number(prompt("It is day " + counter + ". Where will you hide (1, 2, 3, or 4)?")); 
     var explosionLocation = Math.floor(Math.random() * 4) + 1; 
      if (hide <1 || hide >4) { 
       alert("That is not a valid choice.") 
      } 
      else { 
       alert("Hiding place " + explosionLocation + " has exploded!") 
       if (hide !== explosionLocation) { 
        alert("You have survived!") 
        document.writeln("<tr><th>" + counter + "</th><td class='survived'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>") 
       } 
       else { 
       alert("You have died. You survived for a total of " counter - 1 " days.") 
       document.writeln("<tr><th>" + counter + "</th><td class='died'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>"") 
       document.writeln("<tr colspan='3'><td>"'Survived for ' + counter - 1 + ' days""</td></tr>") 
       } 
      } 
    } while (explosionLocation !== hide) 




</script> 

如果有人可以帮助将不胜感激。该代码应该生成一个游戏,用户试图避免死亡的几率为1/4。

+1

您在该行末尾有错字。一个太多的引号。 – 2014-10-31 01:22:25

+1

您应该学会解决您的代码问题。这是编程时你会一直在做的事情。如果您无法发现错误,请尝试在给定区域周围注释行,直到错误消失,然后仔细检查您的语法。 – 2014-10-31 01:24:24

+2

我认为你应该尝试更多的难度,然后在这里发布你的问题 – 2014-10-31 01:24:33

回答

0

这应该是最起码的获取代码运行:

var counter = 0; 
    document.writeln("<table>") 
    document.writeln("<th>Day</th>") 
    document.writeln("<th>Hiding Place</th>") 
    document.writeln("<th>Explosion</th>") 
do{ 
    counter = counter + 1 
    var hide = Number(prompt("It is day " + counter + ". Where will you hide (1, 2, 3, or 4)?")); 
    var explosionLocation = Math.floor(Math.random() * 4) + 1; 
     if (hide <1 || hide >4) { 
      alert("That is not a valid choice.") 
     } 
     else { 
      alert("Hiding place " + explosionLocation + " has exploded!") 
      if (hide !== explosionLocation) { 
       alert("You have survived!") 
       document.writeln("<tr><th>" + counter + "</th><td class='survived'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>") 
      } 
      else { 
      alert("You have died. You survived for a total of " + (counter - 1) + " days.") 
      document.writeln("<tr><th>" + counter + "</th><td class='died'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>") 
      document.writeln("<tr colspan='3'><td>Survived for " + (counter - 1) + " days</td></tr>") 
      } 
     } 
} while (explosionLocation !== hide) 

这是三个关键线路改变是这些:

  alert("You have died. You survived for a total of " + (counter - 1) + " days.") 
      document.writeln("<tr><th>" + counter + "</th><td class='died'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>") 
      document.writeln("<tr colspan='3'><td>Survived for " + (counter - 1) + " days</td></tr>") 
      } 

几个引号不匹配,你需要将括号放在0左右如果你想让它实际上做它的数学。否则,当您将-置于组合字符串的中间时,JavaScript会对您询问的内容感到困惑。

+0

好的谢谢!并且你知道如果结果只有1天,是否有任何方法可以说“日”? – 2014-10-31 02:09:23

+0

您必须将这部分代码包装在if语句中,并检查结果是否仅在决定输出“day”或“days”之前一天。 – KoratDragonDen 2014-10-31 02:39:43

1

你忘了Concat的用+符号的字符串,在过去3个警报

alert("You have died. You survived for a total of " + (counter - 1) + " days.") 
document.writeln("<tr><th>" + counter + "</th><td class='died'>" + hide + "</td><td class='survived'>" + explosionLocation + "</td></tr>") 
document.writeln("<tr colspan='3'><td>Survived for " + (counter - 1) + " days</td></tr>") 
+0

好的谢谢!你知道如果有什么办法让它只是说“日”,如果结果只有1天 – 2014-10-31 02:08:42

相关问题