Javascript 中的 For 循环与 while 循环
For loop vs. while loop in Javascript
完全是初学者...
研究用户杀死龙或被吃掉的盲目概率在线示例 'game'。我知道游戏使用 while 循环运行,所以我尝试使用 for 循环复制它但失败了。我主要好奇为什么 for 循环不起作用,以及是否有一些明显的原因需要使用 while 循环来完成。
下面是 工作 示例,带有 while 循环以提供上下文。
var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;
while (slaying) {
if (youHit) {
console.log("You hit the dragon and did " + damageThisRound + " damage!");
totalDamage += damageThisRound;
if (totalDamage >= 4) {
console.log("You did it! You slew the dragon!");
slaying = false;
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("The dragon burninates you! You're toast.");
slaying = false;
}
}
这里是不工作 for 循环。
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
for(totalDamage=0;totalDamage>3;totalDamage+=damageThisRound){
if(youHit){
console.log("You hit and did "+damageThisRound);
totalDamage += damageThisRound;
if(totalDamage>3){
console.log("You did it! You slew the dragon!");
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("The dragon kills you");
}
}
谢谢
在您的 for
循环中,您将 totalDamage
设置为 0
,然后调用 totalDamage > 3
。相反,将 for
循环更改为
for(totalDamage=0;totalDamage<3;totalDamage+=damageThisRound){
换句话说,你换了一个符号,因为你将变量设置为 0
,然后仅当变量 大于 时才继续 [=17] =].
你的循环条件有问题
var youHit, damageThisRound;
for (var totalDamage = 0; totalDamage < 4; totalDamage += damageThisRound) {
youHit = Math.floor(Math.random() * 2);
if (youHit) {
//need to calculare the damage in each loop
damageThisRound = Math.floor(Math.random() * 5 + 1);
snippet.log("You hit and did " + damageThisRound);
} else {
snippet.log("The dragon kills you");
//you need to stop the loop here
break;
}
}
//need this outside of the loop since the increment is in the for loop block
if (youHit) {
snippet.log("You did it! You slew the dragon!");
}
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
完全是初学者...
研究用户杀死龙或被吃掉的盲目概率在线示例 'game'。我知道游戏使用 while 循环运行,所以我尝试使用 for 循环复制它但失败了。我主要好奇为什么 for 循环不起作用,以及是否有一些明显的原因需要使用 while 循环来完成。
下面是 工作 示例,带有 while 循环以提供上下文。
var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;
while (slaying) {
if (youHit) {
console.log("You hit the dragon and did " + damageThisRound + " damage!");
totalDamage += damageThisRound;
if (totalDamage >= 4) {
console.log("You did it! You slew the dragon!");
slaying = false;
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("The dragon burninates you! You're toast.");
slaying = false;
}
}
这里是不工作 for 循环。
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
for(totalDamage=0;totalDamage>3;totalDamage+=damageThisRound){
if(youHit){
console.log("You hit and did "+damageThisRound);
totalDamage += damageThisRound;
if(totalDamage>3){
console.log("You did it! You slew the dragon!");
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("The dragon kills you");
}
}
谢谢
在您的 for
循环中,您将 totalDamage
设置为 0
,然后调用 totalDamage > 3
。相反,将 for
循环更改为
for(totalDamage=0;totalDamage<3;totalDamage+=damageThisRound){
换句话说,你换了一个符号,因为你将变量设置为 0
,然后仅当变量 大于 时才继续 [=17] =].
你的循环条件有问题
var youHit, damageThisRound;
for (var totalDamage = 0; totalDamage < 4; totalDamage += damageThisRound) {
youHit = Math.floor(Math.random() * 2);
if (youHit) {
//need to calculare the damage in each loop
damageThisRound = Math.floor(Math.random() * 5 + 1);
snippet.log("You hit and did " + damageThisRound);
} else {
snippet.log("The dragon kills you");
//you need to stop the loop here
break;
}
}
//need this outside of the loop since the increment is in the for loop block
if (youHit) {
snippet.log("You did it! You slew the dragon!");
}
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>