2016-07-15 51 views
0

謝謝你回答我原來的問題,我只是編輯這篇文章的原因是因爲我的第二個問題關於這段代碼是因爲該網站不會讓我提出很多問題。我的問題是爲什麼isnt makesjump1隨機爲真或假?它似乎總是成真。請幫助@Yhlas和@codeConcussion爲什麼不在此代碼中的隨機化工作?

var isjumping1 = true; 

while(isjumping1) { 

var makesjump1 = Math.random() 
if(makesjump1 => .51) { 
    makesjump1 = true } 
else if(makesjump1 <= .50) { 
    makesjump1 = false } 

var jump1 = prompt("Do you choose to JUMP, or let the fairies help you FLY").toUpperCase() 
switch(jump1) { 
    case 'JUMP': 
     if(makesjump1 = true) { 
      console.log("You made the jump on your own, so the fairies reward you with a steel sword(9 DMG)") 
      damage = 9; 
      weapon = 'steel sword(9 DMG)'; } 
     else if(makesjump1 = false) { 
      console.log("You attempt the jump but miss it, and are hanging on by a thread") 
      console.log("The fairies rescue you, but you got scratched up, doing 3 damge to you.") 
      health = health - 3; } 
    isjumping1 = false; 
    break; 
    case 'FLY': 
     console.log("The fairies help you over the pit") 
     isjumping1 = false; 
    break; 
    default: 
     alert("That was not a choice!") 
    break; } 
} 
+1

發佈實際代碼時,應該爲語言的內容添加標籤。它影響語法着色並幫助其他人找到問題。 – crashmstr

+0

對不起,我將添加標籤,現在我忘了 –

回答

1

您將其分配給每個循環都爲true。使用==代替或只是......

while(isjumping1) 
0
while(isjumping1==1) - comparison 
while(isjumping1=1) - assignment(always returns true) 
0

,你分配隨機值makesjump1的方式不正確。如果Math.random()返回範圍內的值(0.50,0.51),則會失敗。相反,試試這個:

var makesjump1 = Math.random()<0.5; 
+0

所以如果我這樣做,這將是如果數量小於0.5這將是真實的,並且比它將是假的?感謝您的幫助,我會去替換我的代碼的一部分,看看結果。 –

+0

是的,這就是結果。這是獲得隨機「真」或「假」值的快速方法。 – kamoroso94

相關問題