简体   繁体   中英

Java switch statements

Was just wondering if I could do something like this in java with switch statements?

switch(a && b){

case 1:

//

case 2:

//

}

Sure you can, but not with logical AND (&&). You probably meant to use bit-wise AND (as your case clauses suggest) :

switch(a & b) {
case 1: 
case 2:   
}

What is the data type of a & b ? If it is boolean then it won't work, switch works with int . If they are int then you must use single & operation and not a &&

 public static void main(String[] args) {
    int a,b;
    a=5;
    b=2;
    switch(a & b){

    case 1:

    //

    case 2:

    //

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM