= have a poor comparison in

Asked 2 years ago, Updated 2 years ago, 32 views

I wrote this code again to simplify the intent of the previous question. I can't use control statements such as break because I use TimerTask to control it.Why doesn't it end when L is set to false?

public class Main {

    public static void main(String[]args) {
        boolean K=true;
        boolean L=true;

        while(and(K,L)){
            System.out.println("test");
            L = false;
        }
    }

    public static boolean and (boolean a, boolean b) {
        if(a=true){
            if(b=true){
                return true;
            }
        }
        return false;
    }

}

java

2022-09-30 19:29

2 Answers

= will be substituted.The comparison operator == should work.

 if(a==true){

Or

 if(a){

That's good.


2022-09-30 19:29

= is a substitute for most programming languages, not just Java.
In your code, you set the value of a to true and then look at the contents of a to determine if.
(The same goes for b)

 if(a=true){// The result of the decision is absolutely true only
    // a is absolutely true (of course)
    if(b=true){// The result is definitely only true
    //b is absolutely true (of course)
        return true;
    } else{
    // Again, b is absolutely true.
    }
} else{
// Again, a is absolutely true.
}



// Correct example
if(a==true){
    if(b==true){
        return true;
    }
}

In short, the code you wrote goes down when you break it down

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


2022-09-30 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.