Java 字符串到字符| |字符到字符串

Java 字符串到字符| |字符到字符串,java,android,Java,Android,假设我有一个钮扣 case R.id.button: } // for 它将执行以下功能: int position; String keyInStringForm = et2.getText().toString(); int keyInIntegerForm = Integer.parseInt(keyInStringForm); String text = et1.getText().toString(); for (in

假设我有一个钮扣

case R.id.button:
} // for
它将执行以下功能:

int position;
    String keyInStringForm = et2.getText().toString();
            int keyInIntegerForm = Integer.parseInt(keyInStringForm);
            String text = et1.getText().toString();
    for (int i = 0; i < text.length(); i++) {
                    if (text.charAt(i) == 'a' || text.charAt(i) == 'A') {
                        position = 0;
                        break;
                    } else if (text.charAt(i) == 'b' || text.charAt(i) == 'B') {
                        position = 1;
                        break;
                    } else if (text.charAt(i) == 'c' || text.charAt(i) == 'C') {
                        position = 2;
                        break;
                    } else if (text.charAt(i) == 'd' || text.charAt(i) == 'D') {
                        position = 3;
                        break;
                    } else if (text.charAt(i) == 'e' || text.charAt(i) == 'E') {
                        position = 4;
                        break;
                    } else if (text.charAt(i) == 'f' || text.charAt(i) == 'F') {
                        position = 5;
                        break;
                    } else if (text.charAt(i) == 'g' || text.charAt(i) == 'G') {
                        position = 6;
                        break;
                    } else if (text.charAt(i) == 'h' || text.charAt(i) == 'H') {
                        position = 7;
                        break;
                    } else if (text.charAt(i) == 'i' || text.charAt(i) == 'I') {
                        position = 8;
                        break;
                    } else if (text.charAt(i) == 'j' || text.charAt(i) == 'J') {
                        position = 9;
                        break;
                    } else if (text.charAt(i) == 'k' || text.charAt(i) == 'K') {
                        position = 10;
                        break;
                    } else if (text.charAt(i) == 'l' || text.charAt(i) == 'L') {
                        position = 11;
                        break;
                    } else if (text.charAt(i) == 'm' || text.charAt(i) == 'M') {
                        position = 12;
                        break;
                    } else if (text.charAt(i) == 'n' || text.charAt(i) == 'N') {
                        position = 13;
                        break;
                    } else if (text.charAt(i) == 'o' || text.charAt(i) == 'O') {
                        position = 14;
                        break;
                    } else if (text.charAt(i) == 'p' || text.charAt(i) == 'P') {
                        position = 15;
                        break;
                    } else if (text.charAt(i) == 'q' || text.charAt(i) == 'Q') {
                        position = 16;
                        break;
                    } else if (text.charAt(i) == 'r' || text.charAt(i) == 'R') {
                        position = 17;
                        break;
                    } else if (text.charAt(i) == 's' || text.charAt(i) == 'S') {
                        position = 18;
                        break;
                    } else if (text.charAt(i) == 't' || text.charAt(i) == 'T') {
                        position = 19;
                        break;
                    } else if (text.charAt(i) == 'u' || text.charAt(i) == 'U') {
                        position = 20;
                        break;
                    } else if (text.charAt(i) == 'v' || text.charAt(i) == 'V') {
                        position = 21;
                        break;
                    } else if (text.charAt(i) == 'w' || text.charAt(i) == 'W') {
                        position = 22;
                        break;
                    } else if (text.charAt(i) == 'x' || text.charAt(i) == 'X') {
                        position = 23;
                        break;
                    } else if (text.charAt(i) == 'y' || text.charAt(i) == 'Y') {
                        position = 24;
                        break;
                    } else if (text.charAt(i) == 'z' || text.charAt(i) == 'Z') {
                        position = 25;
                        break;
                    } else if (text.charAt(i) == ' ') {
                        position = 26;
                        break;
                    }
                    int initialResult = position + keyInIntegerForm;
                    int finalResult = initialResult % 26;
                    char resultantChar = alphabets[finalResult];  
} // for
现在将有多个“resultantChar”,我希望这些“resultantChar”组合在一起形成一个字符串,这样我就可以将其设置到文本视图中。
如何使用stringbuilder?您可以使用stringbuilder附加字符。请使用它简化您的代码

} // for
char ch = 'Z';
ch = Character.toLowerCase(ch);
int position = Character.getNumericValue(ch) - Character.getNumericValue('a');
或者,对于您的情况:

} // for
char ch = Character.toLowerCase(text.charAt(i));
if (ch >= 'a' && ch <= 'z') {
    position = Character.getNumericValue(ch) - Character.getNumericValue('a');
} else if (ch == ' ') {
    position = 26;
}
char ch=Character.toLowerCase(text.charAt(i));

如果(ch>='a'&&ch如果我理解正确,请尝试这样做:

} // for
StringBuffer result = new StringBuffer();

for (int i = 0; i < text.length(); i++) {
    ...

char resultantChar = alphabets[finalResult];

result.append(resultantChar);

}

System.out.println(result);
StringBuffer结果=新建StringBuffer();
对于(int i=0;i
哦,是的,你明白了。谢谢你,虽然没那么难;)