题解 | #字符串加解密#
字符串加解密
http://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()) {
encodingsss(sc.nextLine());
// for(int i = 0; i < raws.length; i++) {
// System.out.print(raws[i]);
// }
System.out.println();
// char[] a = encodeStr.toCharArray();
decoding(sc.nextLine());
// for(int i = 0; i < a.length; i++) {
// System.out.print(a[i]);
// }
System.out.println();
}
}
public static void encodingsss(String raws) {
char[] raw = raws.toCharArray();
for(int i = 0; i < raw.length; i++) {
if(raw[i] >= 'a' && raw[i] < 'z') {
raw[i] -= 31;
}
else if(raw[i] == 'z') {
raw[i] = 'A';
}
else if(raw[i] >= 'A' && raw[i] < 'Z') {
raw[i] += 33;
}
else if(raw[i] == 'Z') {
raw[i] = 'a';
}
else if(raw[i] >= '0' && raw[i] < '9') {
raw[i] += 1;
}
else if(raw[i] == '9') {
raw[i] = '0';
}
System.out.print(raw[i]);
}
}
public static void decoding(String raws) {
char[] raw = raws.toCharArray();
for(int i = 0; i < raw.length; i++) {
if(raw[i] > 'a' && raw[i] <= 'z') {
raw[i] -= 33;
}
else if(raw[i] == 'a') {
raw[i] = 'Z';
}
else if(raw[i] >'A' && raw[i] <='Z') {
raw[i] += 31;
}
else if(raw[i] == 'A') {
raw[i] = 'z';
}
else if(raw[i] > '0' && raw[i] <= '9') {
raw[i] -= 1;
}
else if(raw[i] == '0') {
raw[i] = '9';
}
System.out.print(raw[i]);
}
}
}
// We still can use another method to increase the speed, like switch case etc, Thank you four your appreciation