IT박스

Java Switch Statement- "or"/ "and"가 가능합니까?

itboxs 2020. 11. 3. 07:51
반응형

Java Switch Statement- "or"/ "and"가 가능합니까?


char switch 문을 통해 사용할 문자를 찾는 글꼴 시스템을 구현했습니다. 내 글꼴 이미지에 대문자 만 있습니다. 예를 들어 'a'와 'A'가 모두 동일한 출력을 갖도록 만들어야합니다. 사례 수의 2 배를 갖는 대신 다음과 같이 될 수 있습니다.

char c;

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

자바에서 가능합니까?


break;을 생략하여 switch-case fall through를 사용할 수 있습니다 .

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;
}

... 또는 ing 전에 소문자 또는 대문자로 정규화 할 수 있습니다 switch.

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;
}

위의 의미는 AND가 아닙니다. AND의 예 : 110 & 011 == 010 당신이 찾고있는 것이 아닙니다.

OR의 경우 1 일에 중단없이 2 건만 있습니다. 예 :

case 'a':
case 'A':
  // do stuff
  break;

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.


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);

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.

참고URL : https://stackoverflow.com/questions/9883113/java-switch-statement-is-or-and-possible

반응형