为什么这个代码中的随机化不起作用?

时间:2016-07-15 16:53:50

标签: javascript while-loop switch-statement

感谢您回答我原来的问题,我之所以简单地编辑这篇文章是因为我的第二个问题是关于这段代码,因为该网站不会让我提出很多问题。我的问题是为什么不是makejump1随机的真或假?它似乎总是真实的。请帮助@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; }
}

3 个答案:

答案 0 :(得分:1)

您将每个循环分配给true。使用==代替或只是......

while(isjumping1)

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您将随机值分配给makesjump1的方式不正确。如果Math.random()返回范围(0.50,0.51)中的值,则会失败。相反,试试这个:

var makesjump1 = Math.random()<0.5;