I'm making a boolean
so if a certain condition is met, it will return true for 3 seconds, but after that it will return false again.
So it's like
private boolean timedBoolean() {
if(//condition to set boolean true){
return true;
}
//if condition isn't met for 3 seconds
return false;
}
Been thinking about this for an hour now. Researched SO before I asked.
To toggle a boolean, use the strict inequality (! ==) operator to compare the boolean to true , e.g. bool !== true . The comparison will return false if the boolean value is equal to true and vice versa, effectively toggling the boolean.
In the boolean type, there are only two possible values: true and false. We can have variables and expressions of type boolean, just has we have variables and expressions of type int and double. A boolean variable is only capable of storing either the value true or the value false.
boolean user = true; So instead of typing int or double or string, you just type boolean (with a lower case "b"). After the name of you variable, you can assign a value of either true or false. Notice that the assignment operator is a single equals sign ( = ).
Remember the moment at which your 3-second period started:
private volatile long start;
and have the method check the elapsed time:
private boolean timedBoolean() {
if (conditionMet()) {
start = System.nanoTime();
return true;
}
return System.nanoTime()-start < TimeUnit.SECONDS.toNanos(3);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With