2014-10-31 116 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

相關問題