题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
q29 思路: 暴力直接做 a/z或者A/Z做判断 其余字母按+/-32转换大小写
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();
for(int i = 0;i < str1.length();i++){
if(str1.charAt(i) == 'Z')
System.out.print('a');
else if(str1.charAt(i) == 'z')
System.out.print('A');
else if('0' <= str1.charAt(i) && str1.charAt(i) <= '9'){
if(str1.charAt(i) < '9')
System.out.print((char)(str1.charAt(i) + 1));
else
System.out.print('0');
}
else if('A' <= str1.charAt(i) && str1.charAt(i) < 'Z'){
char c = (char)((int)(str1.charAt(i)) + 32 + 1);
System.out.print(c);
}
else if('a' <= str1.charAt(i) && str1.charAt(i) < 'z'){
char c = (char)((int)(str1.charAt(i)) - 32 + 1);
System.out.print(c);
}
else
System.out.print(str1.charAt(i));
}
System.out.println();
for(int i = 0;i < str2.length();i++){
if(str2.charAt(i) == 'A')
System.out.print('z');
else if(str2.charAt(i) == 'a')
System.out.print('Z');
else if('0' <= str2.charAt(i) && str2.charAt(i) <= '9'){
if(str2.charAt(i) > '0')
System.out.print((char)(str2.charAt(i) - 1));
else
System.out.print('9');
}
else if('A' < str2.charAt(i) && str2.charAt(i) <= 'Z'){
char c = (char)((int)(str2.charAt(i)) + 32 - 1);
System.out.print(c);
}
else if('a' < str2.charAt(i) && str2.charAt(i) <= 'z'){
char c = (char)((int)(str2.charAt(i)) - 32 - 1);
System.out.print(c);
}
else
System.out.print(str2.charAt(i));
}
}
}