Java Switch Statement - Is “or”/“and” possible?

前端 未结 5 1778
说谎
说谎 2020-12-14 05:24

I implemented a font system that finds out which letter to use via char switch statements. There are only capital letters in my font image. I need to make it so that, for ex

相关标签:
5条回答
  • 2020-12-14 05:43

    Above, you mean OR not AND. Example of AND: 110 & 011 == 010 which is neither of the things you're looking for.

    For OR, just have 2 cases without the break on the 1st. Eg:

    case 'a':
    case 'A':
      // do stuff
      break;
    
    0 讨论(0)
  • 2020-12-14 06:01

    You can use switch-case fall through by omitting the break; statement.

    char c = /* whatever */;
    
    switch(c) {
        case 'a':
        case 'A':
            //get the 'A' image;
            break;
        case 'b':
        case 'B':
            //get the 'B' image;
            break;
        // (...)
        case 'z':
        case 'Z':
            //get the 'Z' image;
            break;
    }
    

    ...or you could just normalize to lower case or upper case before switching.

    char c = Character.toUpperCase(/* whatever */);
    
    switch(c) {
        case 'A':
            //get the 'A' image;
            break;
        case 'B':
            //get the 'B' image;
            break;
        // (...)
        case 'Z':
            //get the 'Z' image;
            break;
    }
    
    0 讨论(0)
  • 2020-12-14 06:03

    Observations on an interesting Switch case trap --> fall through of switch

    "The break statements are necessary because without them, statements in switch blocks fall through:" Java Doc's example

    Snippet of consecutive case without break:

        char c = 'A';/* switch with lower case */;
        switch(c) {
            case 'a':
                System.out.println("a");
            case 'A':
                System.out.println("A");
                break;
        }
    

    O/P for this case is:

    A

    But if you change value of c, i.e., char c = 'a';, then this get interesting.

    O/P for this case is:

    a A

    Even though the 2nd case test fails, program goes onto print A, due to missing break which causes switch to treat the rest of the code as a block. All statements after the matching case label are executed in sequence.

    0 讨论(0)
  • 2020-12-14 06:06

    The above are all excellent answers. I just wanted to add that when there are multiple characters to check against, an if-else might turn out better since you could instead write the following.

    // switch on vowels, digits, punctuation, or consonants
    char c; // assign some character to 'c'
    if ("aeiouAEIOU".indexOf(c) != -1) {
      // handle vowel case
    } else if ("!@#$%,.".indexOf(c) != -1) {
      // handle punctuation case
    } else if ("0123456789".indexOf(c) != -1) {
      // handle digit case
    } else {
      // handle consonant case, assuming other characters are not possible
    }
    

    Of course, if this gets any more complicated, I'd recommend a regex matcher.

    0 讨论(0)
  • 2020-12-14 06:08

    From what I understand about your question, before passing the character into the switch statement, you can convert it to lowercase. So you don't have to worry about upper cases because they are automatically converted to lower case. For that you need to use the below function:

    Character.toLowerCase(c);
    
    0 讨论(0)
提交回复
热议问题