如何使真假每次之间的布尔变量开关的方法被调用? [英] How to make a boolean variable switch between true and false every time a method is invoked?

查看:148
本文介绍了如何使真假每次之间的布尔变量开关的方法被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个方法调用时,改变一个布尔变量设置为true,并再次调用时,更改为假同一个变量等。

例如:
调用方法 - >布尔=真 - >调用方法 - >布尔=假的 - >调用方法 - >布尔=真

所以基本上,

 如果(A = FALSE){a =真实的; }
如果(A = TRUE){a = FALSE; }

我不知道如何做到这一点,因为我每次调用该方法,布尔值更改为true,然后再假。


解决方案

 值^ = TRUE;

这是值异或等于true,将每一次翻转,并且没有任何分支或临时变量。

I am trying to write a method that when invoked, changes a boolean variable to true, and when invoked again, changes the same variable to false, etc.

For example: call method -> boolean = true -> call method -> boolean = false -> call method -> boolean = true

So basically,

if (a = false) { a = true; }
if (a = true) { a = false; }

I am not sure how to accomplish this, because every time I call the method, the boolean value changes to true and then false again.

解决方案

value ^= true;

That is value xor-equals true, which will flip it every time, and without any branching or temporary variables.

这篇关于如何使真假每次之间的布尔变量开关的方法被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆