Java Switch语句-“或” /“和”可能吗?
问题内容:
我实现了一种字体系统,该字体系统通过char switch语句找出要使用的字母。我的字体图像中只有大写字母。我需要这样做,例如,“ a”和“
A”都具有相同的输出。而不是案件数量的两倍,它可能是如下所示:
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;
}
这在Java中可能吗?
问题答案:
您可以通过省略该break;
语句来使用切换大小写掉线。
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;
}
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;
}