如何在使用StringBuilder时循环切换语句?

时间:2018-04-23 14:50:47

标签: java loops switch-statement stringbuilder

我当前的一组switch语句有效,但是它们占用了太多行,我不确定如何创建一个工作循环来压缩switch语句。

这是我在方法中的当前switch语句(我有六个,以下是递增1)

public static String convert (String str) {

String strb = new StringBuilder(str);

switch (str.charAt(4)) {
            case 'a':
            case 'b':
            case 'c':
                strb.insert(4, 2);
                strb.deleteCharAt(5);
                break;
            case 'd':
            case 'e':
            case 'f':
                strb.insert(4, 3);
                strb.deleteCharAt(5);
                break;
            case 'g':
            case 'h':
            case 'i':
                strb.insert(4, 4);
                strb.deleteCharAt(5);
                break;
            case 'j':
            case 'k':
            case 'l':
                strb.insert(4, 5);
                strb.deleteCharAt(5);
                break;
            case 'm':
            case 'n':
            case 'o':
                strb.insert(4, 6);
                strb.deleteCharAt(5);
                break;
            case 'p':
            case 'q':
            case 'r':
            case 's':
                strb.insert(4, 7);
                strb.deleteCharAt(5);
                break;
            case 't':
            case 'u':
            case 'v':
                strb.insert(4, 8);
                strb.deleteCharAt(5);
                break;
            case 'w':
            case 'x':
            case 'y':
            case 'z':
                strb.insert(4, 9);
                strb.deleteCharAt(5);
                break;
    }
     return strb.toString();
}

我尝试过for循环,但它似乎不起作用。有什么建议吗?

for (index = 4; index < str.length(); index++) {
     switch (str.charAt(index)) {
            case 'a':
            case 'b':
            case 'c':
                strb.insert(index, 2);
                strb.deleteCharAt(index + 1);
                break;
            case 'd':
            case 'e':
            case 'f':
                strb.insert(index, 3);
                strb.deleteCharAt(index + 1);
                break;
            case 'g':
            case 'h':
            case 'i':
                strb.insert(index, 4);
                strb.deleteCharAt(index + 1);
                break;
            case 'j':
            case 'k':
            case 'l':
                strb.insert(index, 5);
                strb.deleteCharAt(index + 1);
                break;
            case 'm':
            case 'n':
            case 'o':
                strb.insert(index, 6);
                strb.deleteCharAt(index + 1);
                break;
            case 'p':
            case 'q':
            case 'r':
            case 's':
                strb.insert(index, 7);
                strb.deleteCharAt(index + 1);
                break;
            case 't':
            case 'u':
            case 'v':
                strb.insert(index, 8);
                strb.deleteCharAt(index + 1);
                break;
            case 'w':
            case 'x':
            case 'y':
            case 'z':
                strb.insert(index, 9);
                strb.deleteCharAt(index + 1);
                break;
     }
}

1 个答案:

答案 0 :(得分:0)

更好的方法是完全删除switch语句,并使用查找表来选择数字:

private static final String DIGIT_LOOKUP = "22233344455566677778889999";
...
int pos = Character.toLowerCase(str.charAt(index)) - 'a';
char digit = DIGIT_LOOKUP.charAt(pos);

Demo.