2016-04-25 66 views
0

我想这样做,当人命中循环100命中退出循环时,生命骰子滚动到0.它目前是如何循环100次。我不太清楚我需要如何解决这个问题,任何提示都会有所帮助。我的代码如下。试图让循环退出,但它目前只是继续循环100次

function addChar(fname, lname, speed, agility, wep, outfit, mood) { 
    this.fname = fname; 
    this.lname = lname; 
    this.speed = speed; 
    this.agility = agility; 
    this.wep = wep; 
    this.outfit = outfit; 
    this.mood = mood; 

    this.special = function(specialMove) { 
     specialMove(); 

    } 

    this.jumpKick = function() { 
     var jmpkckTimes = Math.floor(Math.random() * (100 - 33 + 1)) + 33; 

     document.write("He jumpkicks " + jmpkckTimes + " times. "); 
     if (jmpkckTimes > 70) { 
      document.write("He enters rage mode! "); 
     } else { 
      document.write("He does not have enough kicks for rage mode. "); 
     } 
    } 

    this.hits = function() { 
     var allHits = Math.floor(Math.random() * (100 - 33 + 1)) + 33; 
     document.write(" He gets attacked for " + allHits + " HP."); 
    } 
    this.lifes = function() { 
     var life = Math.floor(Math.random() * (3 - 0 + 1)) + 0; 
     if (life > 0) { 
      document.write(" The life dice rolls a " + life + ". You have survived! For now..."); 


     } else { 
      document.write(" The life dice rolls a " + life + ". You have died!"); 

     } 
    } 
} 
var myChar = new addChar('Johhny', 'Kicker', 10, 7, 'Ancient Greataxe', 'Barrows', 'angry'); 

document.write(myChar.fname + " " + myChar.lname + "'s speed is " + myChar.speed + "<br>"); 
document.write(myChar.fname + " " + myChar.lname + "'s agility is " + myChar.agility + "<br>"); 
document.write(myChar.fname + " " + myChar.lname + "'s weapon of choice is: " + myChar.wep + "<br>"); 
document.write(myChar.fname + " " + myChar.lname + " feels " + myChar.mood + "<br>"); 
document.write(myChar.fname + " " + myChar.lname + " attempts his special: "); 
myChar.special(myChar.jumpKick) 

for (i = 1; i < 101; i++) { 
    myChar.hits(myChar.allHits) 
    myChar.lifes(myChar.lifes) 
} 

function myOutfit() { 
    document.getElementById("demo").innerHTML = ("He is wearing " + myChar.outfit) 

} 
var start = Date.now(); 
var response = prompt("Do you think my character has what it takes?", ""); 
var end = Date.now(); 
var elapsed = (end - start)/1000; 
console.log("You took " + elapsed + " seconds" + " to type: " + response); 

回答

0

以及一般你可以摆脱福尔的藏汉回路为防止福尔循环的进一步执行,并继续下一个迭代:

for (var i = 0; i < 10; i++) { 
    if (i == 4) continue; 
    if (i == 8) break; 
    console.log(i); 
} 

这将基本打印:0,1,2, 3,5,6,7
(你可以看到它那种跳过4)
(它也将在工作一段时间/ while循环)

所以你的情况,你可以检查返回值你的一个f联合是真或假,或做一些其他类型的条件检查,以摆脱循环。

或类似抢布兰德在他的回答如何写道:

var maxTurns = 100; 

var turns = 0; 
while (myChar.isAlive && ++turns <= maxTurns) { 
    myChar.hits(); 
    myChar.lifes(); 
} 
console.log("character is: " + myChar.isAlive ? "alive" : "dead"); 
console.log("after: " + turns + " turns."); 
+0

我已经相应修改我的代码,但我得到了相同的输出。 –

2

你需要有一个沟通方式之外的对象,什么对象内部发生的事情。例如,当某个函数发生某些事情时,例如lifes()或hits(),您应该为对象上的变量赋值以保留状态。这样你可以从for循环访问状态。

例子:

this.isAlive = true; // starting condition 

this.lifes = function() { 
    var life = Math.floor(Math.random() * (3 - 0 + 1)) + 0; 
    this.isAlive = (life > 0); 
    if (this.alive) { 
     document.write('you survived'); 
    } else { 
     document.write('you died'); 
    } 

现在,在你的for循环,您可以访问的IsAlive:

// loop until 100 attempts or you die, which ever comes first 
for (i = 1; i < 101 && myChar.isAlive; i++) { 
    myChar.hits(myChar.allHits) 
    myChar.lifes(myChar.lifes) 
} 
+0

我刚刚尝试了您的更正,但他们似乎没有更正解决方案。所以我需要进一步编辑? –

+1

@DrewStenger这是一个例子,而不是一个实现。您的问题是基于对象的当前状态提前退出循环。这取决于你如何实现该逻辑。 –

+0

@DrewStenger我们不是奴才。此外,SO规则明确规定。 – GottZ