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

前端 未结 5 1803
说谎
说谎 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 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.

提交回复
热议问题