如何在包含charAt()的Switch case中输入String

时间:2017-04-02 19:00:12

标签: java string switch-statement append

我想附加每个数字来表示每个字母,charAt()不允许我使用字符串case语句。请问有关我如何做到这一点的任何想法?我已经将字母附加到数字上。但从数字到字母表,它对我来说是另一回事。我真的需要帮助。

String Sentence;
    Sentence = JOptionPane.showInputDialog(null, "Enter some String", "Message");
    StringBuilder build = new StringBuilder();


    for(int i=0; i<trim.length();i++)
    {
for (int i = 0; i < Sentence.length(); i++) {
    switch (Sentence.charAt(i)) {
        case '1':
            build.append("a");
            break;
        case '2':
            build.append("b");
            break;
        case '3':
            build.append("c");
            break;
        case '4':
            build.append("d");
            break;
        case '5':
            build.append("e");
            break;
        case '6':
            build.append("f");
            break;
        case '7':
            build.append("g");
            break;
        case '8':
            build.append("h");
            break;
        case '9':
            build.append("i");
            break;
        case (
            '1.0'):
            build.append("j");
            break;
        case "1.1":
            build.append("k");
            break;
        case "1.2":
            build.append("l");
            break;
        case "1.3":
            build.append("m");
            break;
        case "1.4":
            build.append("n");
            break;
        case "1.5":
            build.append("o");
            break;
        case "1.6":
            build.append("p");
            break;
        case "1.7":
            build.append("q");
            break;
        case "1.8":
            build.append("r");
            break;
        case "1.9":
            build.append("s");
            break;
        case "2.0":
            build.append("t");
            break;
        case "2.1":
            build.append("u");
            break;
        case "2.2":
            build.append("v");
            break;
        case "2.3":
            build.append("w");
            break;
        case "2.4":
            build.append("x");
            break;
        case "2.5":
            build.append("y");
            break;
        case "2.6":
            build.append("z");
            break;
        case '?':
            build.append(" ");
            break;
        case '*':
            build.append(",");
            break;
        case "'\'":
            build.append('.');
            break;
        default:

            break;
    }

严重的任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:1)

要快速修改为工作版本 - 只需将您的字符转换为String并在切换中使用它:

 switch ("" + Sentence.charAt(i)) {
    case "a": // "a" is a String now
...