2017-06-08 39 views
-2

添加break後,我的代碼不再有效。這次休息時寫了些什麼;

alert("Warning: Game is made for the age of 18 and over."); 
var a = prompt("Are you ready to enter this game?"); 
if (a == "yes") {alert("Entering");} 
else if (a == "no") {while(true) 
var a = prompt("Are you ready to enter this game?"); 
if(a == "yes") {break; alert("Entering");} } 
+1

你想通過在那裏休息來完成什麼?也許嘗試查找使用示例。這是不正確的。 – hack3rfx

+0

停止while(true) – OkCoder

+1

,但您在警報之前停止它(「輸入」)。你的戒備永遠不會達到。如果這是你唯一缺少的東西,那就試着做一下alert(「Entering」);打破; – hack3rfx

回答

0

您在JavaScript中有幾個語法錯誤。你錯過了大括號。 break聲明需要在alert聲明之後。修復後請查看以下片段,並嘗試瞭解您的錯誤。

alert("Warning: Game is made for the age of 18 and over."); 
 
var a = prompt("Are you ready to enter this game?"); 
 
if (a == "yes") {alert("Entering");} 
 
else if (a == "no") { 
 
    while(true) { 
 
    //var b = prompt("Are you ready to enter this game?"); 
 
    if(a == "yes") { 
 
     alert("Entering"); 
 
     break; 
 
     } 
 
    } 
 
}

+0

這不符合目的 – OkCoder

+0

如果它不,我誤解了你的目的。請詳細說明你想達到的目標。讓你確定你不代表一個XY問題。 – VHS

0

我相信你打算反覆提示用戶,直到他回答yes。在這種情況下,使用此代碼:

alert("Warning: Game is made for the age of 18 and over."); 
 

 
let response = ""; 
 
while(response !== "yes") { 
 
    response = prompt("Are you ready to enter this game?"); 
 
} 
 

 
alert("Entering");

+0

你能解釋一下嗎? – OkCoder

0
break; alert("Entering"); 

它的後期到alertbreak荷蘭國際集團,它立即退出循環後本身不執行指令後那裏。