Java 每次调用方法时如何使布尔变量在 true 和 false 之间切换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2479058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:13:19  来源:igfitidea点击:

How to make a boolean variable switch between true and false every time a method is invoked?

javaboolean

提问by aperson

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.

我正在尝试编写一个方法,该方法在调用时将布尔变量更改为 true,再次调用时将同一变量更改为 false,等等。

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

例如:调用方法->boolean=true->调用方法->boolean=false->调用方法->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.

我不确定如何实现这一点,因为每次我调用该方法时,布尔值都会更改为 true,然后再次更改为 false。

回答by Randy Simon

Just toggle each time it is called

每次调用时只需切换

this.boolValue = !this.boolValue;

回答by rayd09

private boolean negate(boolean val) {
    return !val;
}

I think that is what you are asking for??

我想这就是你要的??

回答by JoePasq

Without looking at it, set it to not itself. I don't know how to code it in Java, but in Objective-C I would say

不看它,将它设置为不是它自己。我不知道如何用 Java 编码,但在 Objective-C 中我会说

booleanVariable = !booleanVariable;

This flips the variable.

这会翻转变量。

回答by bobDevil

Assuming your code above is the actual code, you have two problems:

假设你上面的代码是实际代码,你有两个问题:

1) your if statements need to be '==', not '='. You want to do comparison, not assignment.

1) 你的 if 语句需要是 '==',而不是 '='。你想做比较,而不是赋值。

2) The second if should be an 'else if'. Otherwise when it's false, you will set it to true, then the second if will be evaluated, and you'll set it back to false, as you describe

2)第二个if应该是一个'else if'。否则,当它为 false 时,您将其设置为 true,然后将评估第二个 if,然后将其设置回 false,如您所描述的

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

Another thing that would make it even simpler is the '!' operator:

另一件让它变得更简单的事情是“!” 操作员:

a = !a;

will switch the value of a.

将切换 a 的值。

回答by ILMTitan

value ^= true;

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

即值 xor-equals true,每次都会翻转它,并且没有任何分支或临时变量。

回答by whirlwin

回答by GFP

I do it with boolean = !boolean;

我这样做 boolean = !boolean;

回答by EvilInside

var logged_in = false;
logged_in = !logged_in;

A little example:

一个小例子:

var logged_in = false;


$("#enable").click(function() {
    logged_in = !logged_in;
    checkLogin();
});

function checkLogin(){
    if (logged_in)
        $("#id_test").removeClass("test").addClass("test_hidde");
    else
        $("#id_test").removeClass("test_hidde").addClass("test");
    $("#id_test").text($("#id_test").text()+', '+logged_in);
}
.test{
    color: red;
    font-size: 16px;
    width: 100000px
}

.test_hidde{
    color: #000;
    font-size: 26px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test" id="id_test">Some Content...</div>
<div style="display: none" id="id_test">Some Other Content...</div>


<div>
    <button id="enable">Edit</button>
</div>

回答by bmavus

in java when you set a value to variable, it return new value. So

在 java 中,当您将值设置为变量时,它会返回新值。所以

private boolean getValue()
{
     return value = !value;
}