2015-09-23 187 views
-1

我不明白爲什麼,但我的循環將無法完成。我應該使用JavaScript做一個猜謎遊戲,但循環不會實際循環。它只需要一個答案,顯示適當的窗口並停下來。不管答案如何。下面是代碼:while循環嵌套if語句不會循環javascript

<!DOCTYPE > 
<html> 
<head> 
<title>Welcome To The JS Guessing Game</title> 
</head> 
<script> 

//Initialize the variables that will be needed. 
var target; 
var target_index; 
var guess_input; 
var guesses=0; 
var finished=false; 
var guesses=0; 
var colors = ["blue", "cyan", "gold", "green", "magenta", "orange", "red", "white", "yellow"]; 

function do_game() { 
    var random_number = (Math.random()*colors.length); 
    var random_number=Math.floor(random_number); 
    target=colors[random_number]; 
    alert(target); 
    while (!finished){ 
     guess_input=prompt("I am thinking of a color in the list below. Can you guess which color?"+"\n\n"+ colors.join()); 
     guesses++; 
     if (guess_input!=target){ 
      alert("no") 
      return true; 
     } 
     alert("yes!!") 
     return false; 
    } 
} 
</script> 
<body onload="do_game()"> 
</body> 
</html> 

現在我真不明白我在做什麼錯誤,並在控制檯標誌了沒有錯誤在任何瀏覽器:/ 我會很感激的幫助!

乾杯,

大衛

+1

設置'finished'到TRUE;在循環... – tymeJV

+2

的'返回TRUE;退出功能,因而不會繼續循環。你可以用'continue'或者'else'結構替換它。使用返回使完成的變量過時,所以你可以只使用while(true) –

回答

0

的問題是,你打破了while循環輸入了不正確的答案時,你運行該代碼:不是返回

if (guess_input!=target){ 
    alert("no") 
    return true; 
} 

相反一個值,考慮使用continue聲明。

if (guess_input!=target){ 
    alert("no") 
    continue; 
} 

這將跳轉到循環的下一次迭代,住在循環中,但不執行alert("yes!!")。這裏有一個JSFiddle來演示。希望這可以幫助!如果您有任何問題,請告訴我。

(而按你的問題的意見,你並不真正需要的finished變量,只是用while(true)應該足夠了。)

0

設置finishedtrue(無需將其設置爲false因爲它會直到你改變它,一直是false)。如果你需要在循環之後返回finished(我認爲你不這樣做),我在聲明中加入了這個效果。您還需要在此處有else子句,否則即使條件滿足,您的代碼中的finished也將設置爲true

function do_game() { 
    var random_number = (Math.random()*colors.length); 
    var random_number=Math.floor(random_number); 
    target=colors[random_number]; 
    while (!finished){ 
     guess_input=prompt("I am thinking of a color in the list below. Can you guess which color?"+"\n\n"+ colors.join()); 
     guesses++; 
     if (guess_input!=target){ 
      alert("no"); 
     } else { 
      alert("yes!!"); 
      finished = true; 
     } 
    } 
    return finished; 
} 
0

無論條件如何,您都返回了布爾值。就拿這裏看起來代碼:https://jsfiddle.net/zmdcL4h4/1/

//Initialize the variables that will be needed. 
var target; 
var target_index; 
var guess_input; 
var guesses=0; 
var finished=false; 
var guesses=0; 
var colors = ["blue", "cyan", "gold", "green", "magenta", "orange", "red", "white", "yellow"]; 

function do_game() { 
    var random_number = (Math.random()*colors.length); 
    var random_number=Math.floor(random_number); 
    target=colors[random_number]; 
    alert(target); 
    while (!finished){ 
     guess_input=prompt("I am thinking of a color in the list below. Can you guess which color?"+"\n\n"+ colors.join()); 
     if(!guess_input){ 
      finished = true; 
      return true; 
     } 
     guesses++; 
     if (guess_input!=target){ 
      alert("no") 
     } 
     else 
      return true; 
    } 
} 
+0

謝謝,但你的代碼顯示不是和是的。我也希望當它達到正確的答案時停止。 – Kingdavidek

+0

我更新了代碼。你不清楚你想要發生什麼 – jnoreiga