题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
import java.util.Scanner; import java.lang.StringBuilder; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String a = in.nextLine(); String b = in.nextLine(); showCodeResult(a, b); } } private static void showCodeResult(String a, String b) { char[] arr1 = a.toCharArray(); char[] arr2 = b.toCharArray(); StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); for (int i = 0; i < arr1.length; i++) { if (Character.isUpperCase(arr1[i])) { if (arr1[i] == 'Z') { sb1.append('a'); } else { sb1.append((char)(arr1[i] + 1 + 32)); } } else if (Character.isLowerCase(arr1[i])) { if (arr1[i] == 'z') { sb1.append('A'); } else { sb1.append((char)(arr1[i] + 1 - 32)); } } else { if (arr1[i] == '9') { sb1.append('0'); } else { sb1.append(arr1[i] - '0' + 1); } } } System.out.println(sb1.toString()); for (int i = 0; i < arr2.length; i++) { if (Character.isUpperCase(arr2[i])) { if (arr2[i] == 'A') { sb2.append('z'); } else { sb2.append((char)(arr2[i] - 1 + 32)); } } else if (Character.isLowerCase(arr2[i])) { if (arr2[i] == 'a') { sb2.append('Z'); } else { sb2.append((char)(arr2[i] - 1 - 32)); } } else { if (arr2[i] == '0') { sb2.append('9'); } else { sb2.append(arr2[i] - '0' - 1); } } } System.out.println(sb2.toString()); } }