0

我想检查当前字符是否为空格,因为我不想更改它。所以我想像现在一样更改所有字母,但保留空格。

例如,C = 2 的“那个人”应该变成“vjcv ocp”。

提前致谢 :)

    String initialString = yourString.getText().toString();
        char[] chars = initialString.toCharArray();
        for (int i = 1; i <= chars.length; i++) {
            C = Integer.valueOf(ceasarNr);

            chars[i-1] = characters.get((characters.indexOf(chars[i-1]) + C)%29);
        }
        String resultString = new String(chars);
        krypteredeTekst.setText(resultString);
    }
4

2 回答 2

0

如果字符是空格,则跳过迭代:

String initialString = yourString.getText().toString();
    char[] chars = initialString.toCharArray();
    for (int i = 1; i <= chars.length; i++) {
        //Skip if it is space.
        if chars[i-1] == ' ';
             continue;

        C = Integer.valueOf(ceasarNr);

        chars[i-1] = characters.get((characters.indexOf(chars[i-1]) + C)%29);
    }
    String resultString = new String(chars);
    krypteredeTekst.setText(resultString);
}
于 2013-05-05T20:30:37.630 回答
0

有检查char是否为空格的方法->

    String initialString = yourString.getText().toString();
    char[] chars = initialString.toCharArray();
    for (int i = 1; i <= chars.length; i++) {
        C = Integer.valueOf(ceasarNr);

        if(!Character.isWhitespace(chars.charAt(i-1)) {
            chars[i-1] = characters.get((characters.indexOf(chars[i-1]) + C)%29);
        }
    }
    String resultString = new String(chars);
    krypteredeTekst.setText(resultString);
于 2013-05-05T20:35:29.603 回答