题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
循环除法:(value + radix) % radix
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println(convert(in.nextLine(), 1));
System.out.println(convert(in.nextLine(), -1));
}
private static StringBuilder convert(String str, int type) {
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray()) {
if (Character.isUpperCase(c)) {
sb.append(convert(c, 'A', 'a', 26, type));
} else if (Character.isLowerCase(c)) {
sb.append(convert(c, 'a', 'A', 26, type));
} else if (Character.isDigit(c)) {
sb.append(convert(c, '0', '0', 10, type));
}
}
return sb;
}
private static char convert(char c, char before, char after, int radix, int type) {
return (char) ((c - before + type + radix) % radix + after);
}
}