2016-07-15 54 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

相关问题