我已经编写了这个代码来加密文本并将新文本放置在TextView中。当时我正在想办法做几件事。
发布于 2013-04-29 09:33:40
我相信你想对前三个字母中的每个字母使用不同的Ceasar键,然后对下一组的三个字母重复相同的Ceasar键,对吧?
我并不是百分之百确定如何处理的不仅仅是字母表中的26个标准字符,但是处理这个问题的一种方法是按照列表中的字符顺序列出一个列表。如果这样做,请确保将字符串转换为所有小写,以便如果有大写,则不会得到匹配错误。
请不要,我无法测试下面的代码,所以很容易就会出现错误(包括语法错误),我认为它应该可以工作,但是您应该能够使它更优雅(例如,for中的那些IFs都有点难看),一些thigns应该被移到他们自己的方法中去。
List<String> characters = new ArrayList<String>();
...Fill the List with the alphabet in order here (best to do in it's own method)
String initialString = yourString.getText().toString().toLowercase();
char[] chars = initialString.toCharArray();
for (int i = 1; i <= chars.length; i++) {
C = Integer.valueOf(ceasarNr1);
if ( i%2 ==0 ) C = Integer.valueOf(ceasarNr2);
if ( i%3 ==0 ) C = Integer.valueOf(ceasarNr3);
chars[i-1] = characters.get((characters.indexOf(chars[i-1]) + C)%29);
}
String resultString = new String(chars);
krypteredeTekst.setText(resultString);https://stackoverflow.com/questions/16274913
复制相似问题