题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String toEncrypt = scan.nextLine();
String toDeciphering = scan.nextLine();
char[] chrs1 = toEncrypt.toCharArray();
char[] chrs2 = toDeciphering.toCharArray();
StringBuffer sb1 = new StringBuffer("");
StringBuffer sb2 = new StringBuffer("");
for (char chr : chrs1) {
if (chr >= 'a' && chr <= 'z') {
if (chr == 'z') {
sb1.append('A');
} else {
sb1.append(String.valueOf((char) (chr + 1)).toUpperCase());
}
} else if (chr >= 'A' && chr <= 'Z') {
if (chr == 'Z') {
sb1.append('a');
} else {
sb1.append(String.valueOf((char) (chr + 1)).toLowerCase());
}
} else {
if (chr == '9') {
sb1.append('0');
} else {
sb1.append((char) (chr + 1));
}
}
}
for (char chr : chrs2) {
if (chr >= 'A' && chr <= 'Z') {
if (chr == 'A') {
sb2.append('z');
} else {
sb2.append(String.valueOf((char) (chr - 1)).toLowerCase());
}
} else if (chr >= 'a' && chr <= 'z') {
if (chr == 'a') {
sb2.append('Z');
} else {
sb2.append(String.valueOf((char) (chr - 1)).toUpperCase());
}
} else {
if (chr == '0') {
sb2.append('9');
} else {
sb2.append((char) (chr - 1));
}
}
}
System.out.println(sb1);
System.out.println(sb2);
}
}