是否有布尔值的赋值后运算符? [英] Is there a post-assignment operator for a boolean?
本文介绍了是否有布尔值的赋值后运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中你可以这样吗?
Hi is something like this possible in Java?
boolean flag = true;
if(flag) return flag = false; // return true and assign false to flag afterwards
澄清。以上作品,但首先是假的。我希望实现的是在 true
后立即返回标志,然后将其重置为 false
。
To clarify. The above works, but is assigns false first. Want I want to achieve is to return the flag as soon as its true
and reset it to false
afterwards.
结构如下所示:
boolean flag = false;
// some operations which can set the flag true
if(flag){ flag = false ; return true};
// some operations which can set the flag true
if(flag){ flag = false ; return true};
// some operations which can set the flag true
if(flag){ flag = false ; return true};
我一直在考虑通过一次性做到这一点返回flag = false ;
推荐答案
不,没有任何内置可以完成您所描述的内容。你可以使用一个临时变量:
No, there's nothing built-in that does what you describe. You'd do it with a temporary variable:
boolean flag = true;
boolean returnValue = flag;
flag = false;
return returnValue;
或根据进一步编辑问题(结构看起来像这个),你可以使用!
:
Or based on your further edit to the question ("The structure looks something like this"), you can use !
:
boolean flag = false;
// some operations which can set the flag true
if(flag) return !(flag = false);
// some operations which can set the flag true
if(flag) return !(flag = false);
// some operations which can set the flag true
if(flag) return !(flag = false);
我真的,真的不会那么做。这是不必要的迟钝。
I really, really would not do that. It's unnecessarily obtuse.
这篇关于是否有布尔值的赋值后运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文