有条件地改变“for循环”的方向?
是否可以有条件地更改 ActionScript 中 for 循环的方向?
示例:
for(if(condition){var x = 0; x<number; x++}else{var x=number; x>0; x--}){
//do something
}
Is it possible to conditionally change the direction of a for loop in ActionScript?
Example:
for(if(condition){var x = 0; x<number; x++}else{var x=number; x>0; x--}){
//do something
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有趣的要求。保留 for 的一种方法是:
我们设置起始值、终止条件的函数以及正增量或负增量。然后,我们只需调用该函数并使用 += 来进行递增或递减。
Interesting requirement. One way to keep the for is:
We setup the start value, a function for the termination condition, and either a positive or negative increment. Then, we just call the function and use
+=
to the do the increment or decrement.ActionScript 有三元运算符,因此您可以执行以下操作:
但这非常难看。 :-)
您可能还需要/想要在其中的各个部分周围放置一些括号。我不确定运算符的优先级。
您还可以考虑使用高阶函数。想象一下你有:
那么你可以这样做:
ActionScript has the ternary operator, so you could do something like:
But this is pretty ugly. :-)
You might also need/want to put some parens around pieces of that. I'm not sure about the operator precedence.
You might also consider using a higher order function. Imagine you have:
Then you could do:
您可能需要一个
while
循环:如果您确实想要一个 for 循环,则应该使用其中两个:
You probably want a
while
loop instead:If you really want a for loop, you should use two of them: