如果不为 true 则重新分配布尔值

发布于 2024-10-21 19:01:01 字数 358 浏览 9 评论 0原文

我有一个方法 foo() 和 foobar() 都返回一个布尔值。无论结果如何,都必须执行它们中的每一个。

boolean changed = true;
while(changed) {
    changed = foo();
    if(!changed) {
        changed = foobar();
    }
    else {
        foobar();
    }
}

我希望只要更改为真,循环就继续执行,但我觉得 foobar() 的 ifs 和 elses 的第二个代码块不是很...优雅。是否有更好的方法来编写该部分,以便更改的变量只有在尚未为真时才会重新分配?

谢谢!

I have a method foo() and foobar() that both return a boolean. Each of them must be executed regardless of the result.

boolean changed = true;
while(changed) {
    changed = foo();
    if(!changed) {
        changed = foobar();
    }
    else {
        foobar();
    }
}

I want the loop to keep executing for as long as changed is true, but I feel like the second code block of ifs and elses for foobar() is not very... elegant. Is there a better way to write that part so that the variable changed will only be reassigned if it's not already true?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

千鲤 2024-10-28 19:01:01

怎么样:

changed = foo() | foobar();

注意按位或运算符的使用。

How about:

changed = foo() | foobar();

Note the use of the bitwise or operator.

错々过的事 2024-10-28 19:01:01

只要更改为真,我希望循环继续执行
是什么意思?
如果两个方法都返回 false,你想停止循环吗
如果是,则执行:

boolean changed = true;
boolean changed1 = true;

    while(changed || changed1) {
        changed = foo();
        changed1 = foobar();
    }

I want the loop to keep executing for as long as changed is true
means?
do u want to stop loop if both method returns false
if yes then do:

boolean changed = true;
boolean changed1 = true;

    while(changed || changed1) {
        changed = foo();
        changed1 = foobar();
    }
把昨日还给我 2024-10-28 19:01:01

除了按位或选项之外,您还可以确保将 changed 放在表达式中的第二个位置,并且所有方法都将被执行:

changed = foo();
changed = bar() || changed;
changed = baz() || changed;

我更喜欢按位选项,因为它表明这些方法具有必要的副作用。上述内容应该有详细的记录,以防止有人稍后出现并“修复它以提高性能”。

Besides the bitwise OR option, you can also just make sure you put changed second in the expression and all the methods will be executed:

changed = foo();
changed = bar() || changed;
changed = baz() || changed;

I like the bitwise option better since it communicates that the methods have necessary side effects. The above should be well documented to prevent somebody from coming along later and "fixing it to be more performant."

不知所踪 2024-10-28 19:01:01

基本上,只要 foo() 或 foobar() 之一返回 true,您就希望继续执行循环。

boolean time_to_stop = false;
while (!time_to_stop) {
    boolean foo_result = foo();
    boolean foobar_result = foobar();
    if (false == (foo_result || foobar_result) ) {
        time_to_stop = true;
    }
}

您可以通过使用语法上等效的代码结构来缩短此长度:

boolean keep_going = true;
while (keep_going) {
    keep_going = foo() | foobar(); 
    // bit-wise OR, avoids compiler short-circuiting optimization
}

最终的简写:

while ( foo() | foobar() ) {};

Basically you want, so long as one of foo() or foobar() return true, to continue executing the loop.

boolean time_to_stop = false;
while (!time_to_stop) {
    boolean foo_result = foo();
    boolean foobar_result = foobar();
    if (false == (foo_result || foobar_result) ) {
        time_to_stop = true;
    }
}

You can shorten this by using syntactically equivalent code structures:

boolean keep_going = true;
while (keep_going) {
    keep_going = foo() | foobar(); 
    // bit-wise OR, avoids compiler short-circuiting optimization
}

To the ultimate short-hand:

while ( foo() | foobar() ) {};
爺獨霸怡葒院 2024-10-28 19:01:01

您可以使用下面

boolean changed = true;
while(changed) {
    changed = (foo())?foobar():(true|foobar());
}

我编辑的答案的缩短代码语法。感谢大家指出错误:)

You can use below shortened code styntax

boolean changed = true;
while(changed) {
    changed = (foo())?foobar():(true|foobar());
}

I edited the answer. Thanks all for pointing out the error :)

翻了热茶 2024-10-28 19:01:01

使用 do-while 的一个很好的例子:

boolean changed;    // no initial value needed
do {
    changed = foo() | foobar();  // should not be ||
} while(changed);

a nice case for using the do-while:

boolean changed;    // no initial value needed
do {
    changed = foo() | foobar();  // should not be ||
} while(changed);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文