如何用&&而不是||分割if()

Dan*_*ika -3 c++ if-statement

我想在c ++中做这样的事情

If x is equal to either a or b or c
and y is equal to either d or e or f
and z is equal to either g or h or i, it would turn true and execute the code
Run Code Online (Sandbox Code Playgroud)

我有点迷失了

if(x==a||x==b||x==c && y==d||y==e||y==f && z==g||z==h||z==i){
 // Do x
}
Run Code Online (Sandbox Code Playgroud)

Bar*_*mar 5

只需使用,&&||使用括号使分组清晰.

if ((x == 'a' || x == 'b' || x == 'c') 
    && (y == 'd' || y == 'e' || y == 'f')
    && (z == 'g' || z == 'h' || z == 'i')) {
    // execute code
}
Run Code Online (Sandbox Code Playgroud)

  • 哦,我觉得很蠢.我怎么没想到使用括号lol.谢谢 (2认同)