题解 | 字符串加解密
重点:
- 牛马活,纯粹的匹配赋值
- 需要仔细写
- 还有一些其他的版本:更简洁
- 字母表直接写出来,一一对应即可
- 使用Charactor的静态方法做计算等
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] enCharArr = in.next().toCharArray(); char[] deCharArr = in.next().toCharArray(); for (int i = 0; i < enCharArr.length; i++) { char c = enCharArr[i]; char tmp = 0; if (c == 'Z') { tmp = 'a'; } if (c == 'z') { tmp = 'A'; } if (c >= 'A' && c < 'Z') { tmp = (char)(c + 33); } if (c >= 'a' && c < 'z') { tmp = (char)(c - 31); } if (c >= '0' && c <= '8') { tmp = (char)(c + 1); } if (c == '9') { tmp = '0'; } enCharArr[i] = tmp; } System.out.println(new String(enCharArr)); for (int i = 0; i < deCharArr.length; i++) { char c = deCharArr[i]; char tmp = 0; if (c == 'a') { tmp = 'Z'; } if (c == 'A') { tmp = 'z'; } if (c >= 'b' && c <= 'z') { tmp = (char)(c - 33); } if (c >= 'B' && c <= 'Z') { tmp = (char)(c + 31); } if (c >= '1' && c <= '9') { tmp = (char)(c - 1); } if (c == '0') { tmp = '9'; } deCharArr[i] = tmp; } System.out.println(new String(deCharArr)); } }