开发者

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

I am trying to write a method tha开发者_如何学运维t 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.


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

booleanVariable = !booleanVariable;

This flips the variable.


Just toggle each time it is called

this.boolValue = !this.boolValue;


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.

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

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.


I do it with boolean = !boolean;


value = (value) ? false : true;

Conditional (ternary) Operator.


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>


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

I think that is what you are asking for??


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

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


here are some ways to do it, pick the one you like:

//set a bool variable to true
bool myBool = true;
print (myBool); //:true

//set 'myBool' to not itself
myBool = !myBool;
print (myBool); //:false

//ternary myBool: if it's true return false, if it's false return true
myBool = myBool ? false : true;
print (myBool); //:true

//ternary !myBool, same as last one but inverted (because why not)
myBool = !myBool ? true : false;
print (myBool); //:false

//set myBool to not itself and true
myBool = !(myBool && true)
print (myBool); //:true

Jokes aside, I always wanted a function to call like myBool.switch() to set it to not itself and return the new value, can't find any downsides to this one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜