In the expression of a while loop, is it possible to initialise a variable, then use that as part of the expression?
这可能比较简单:
while (int a = someMethod(), a<b)
可能只是增加另一种方法,因此必须:
private boolean whileLoopTest() {
int a = someMethod();
return a<b;
}
public void originalMethod() {
while (whileLoopTest()) {
//...
但这似乎只是一线。
EDIT I also don t want to directly compare the method to my variable, as it is compared to several variable, and so if would be a long, unreadable mess. A better example of what I want would be:
while (int a = SomeClass.someStaticMethod(), -1<a && a<b)
It s not true in my case, but this would be a equally valid question if someStaticMethod()
took a long time to return - I would only want to call it once.
I m fairly new to StackOverflow, so I m not sure if giving other situations where this would apply is what I should be doing.