题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String enstr = sc.nextLine(); String destr = sc.nextLine(); StringBuilder ensb = new StringBuilder(); StringBuilder desb = new StringBuilder(); encode(enstr, ensb); decode(destr, desb); System.out.println(ensb); System.out.println(desb); } } private static void encode(String enstr, StringBuilder ensb) { for (int i = 0; i < enstr.length(); i++) { if (enstr.charAt(i) >= 'a' && enstr.charAt(i) < 'z') { ensb.append(Character.toUpperCase((char)(enstr.charAt(i) + 1))); } else if (enstr.charAt(i) == 'z') { ensb.append('A'); } else if (enstr.charAt(i) >= 'A' && enstr.charAt(i) < 'Z') { ensb.append(Character.toLowerCase((char)(enstr.charAt(i) + 1))); } else if (enstr.charAt(i) == 'Z') { ensb.append('a'); } else if (enstr.charAt(i) >= '0' && enstr.charAt(i) < '9') { ensb.append((char)(enstr.charAt(i) + 1)); } else if (enstr.charAt(i) == '9') { ensb.append('0'); } } } private static void decode(String destr, StringBuilder desb) { for (int i = 0; i < destr.length(); i++) { if (destr.charAt(i) > 'a' && destr.charAt(i) <= 'z') { desb.append(Character.toUpperCase((char)(destr.charAt(i) - 1))); } else if (destr.charAt(i) == 'a') { desb.append('Z'); } else if (destr.charAt(i) > 'A' && destr.charAt(i) <= 'Z') { desb.append(Character.toLowerCase((char)(destr.charAt(i) - 1))); } else if (destr.charAt(i) == 'A') { desb.append('z'); } else if (destr.charAt(i) > '0' && destr.charAt(i) <= '9') { desb.append((char)(destr.charAt(i) - 1)); } else if (destr.charAt(i) == '0') { desb.append('9'); } } } }