在 for 循环的开头声明有限范围的变量
在Java中,有时你会做这样的事情:
for (int a = 1, b = 2; b < high;) {
if (b % 2 == 0) {
result += b;
}
int tmp = b;
b = a + b;
a = tmp;
}
这里,我使用for循环而不是while循环来限制a和b的范围。
但我怎样才能在 JavaFX 中实现这一点呢? for循环似乎没有提供这种可能性。我必须使用 while 循环吗?
In Java, you sometimes do something like this:
for (int a = 1, b = 2; b < high;) {
if (b % 2 == 0) {
result += b;
}
int tmp = b;
b = a + b;
a = tmp;
}
Here, I used a for loop instead of a while loop to limit the scope of a and b.
But how can I achieve this in JavaFX? The for loop doesn't seem to offer this possibility. Do I have to use a while loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用匿名块的 Java 技巧:
JavaFX 中有类似的表达式,但使用这些表达式您将获得双循环。根据此doc。
You could use the Java trick of anonymous blocks:
The are simmilar expressions in JavaFX but with those expressions you will get a double loop. According to this doc.