本文介绍了Java开关语句 - 是“或”/“和”可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个字体系统,通过char switch语句找出要使用哪个字母。我的字体图像中只有大写字母。我需要使它,例如,'a'和'A'都有相同的输出。而不是具有2倍的情况,可以是如下:

  char c; 

switch(c){
case'a'& 'A':/ *获取'A'图像* /;打破;
case'b'& 'B':/ *获取'B'图像* /;打破;
...
case'z'& 'Z':/ *获取'Z'图像* /;打破;
}

这在java中是否可能?

break; 语句来使用开关情况下的通过。

  char c = / * whatever * /; 

switch(c){
case'a':
case'A':
//获取'A'
break;
case'b':
case'B':
//获取'B'图像;
break;
//(...)
case'z':
case'Z':
//获取'Z'
break;
}

...或者您只需要标准化或。 / p>

  char c = Character.toUpperCase(/ * whatever * /); 

switch(c){
case'A':
//获取'A'图像;
break;
case'B':
//获取'B'图像;
break;
//(...)
case'Z':
//获取'Z'图像;
break
}


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 example, 'a' and 'A' both have the same output. Instead of having 2x the amount of cases, could it be something like the following:

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

Is this possible in java?

解决方案

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

这篇关于Java开关语句 - 是“或”/“和”可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:43
查看更多