题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); scanner.useDelimiter("\n"); while (scanner.hasNext()) { String S = scanner.next(); String T = scanner.next(); jiami(S); jiemi(T); } } public static void jiami(String S) { for (int i = 0; i < S.length(); i++) { char c = S.charAt(i); if (Character.isLetter(c)) { System.out.print(change(c)); } else if (Character.isDigit(c)){ System.out.print(change(c)); } else { System.out.print(c); } } System.out.println(); } public static void jiemi(String S) { for (int i = 0; i < S.length(); i++) { char c = S.charAt(i); if (Character.isLetter(c)) { System.out.print(change2(c)); } else if (Character.isDigit(c)){ System.out.print(change2(c)); } else { System.out.print(c); } } System.out.println(); } public static char change(char c) { if (Character.isLetter(c)) { if (c=='Z') return 'a'; else if (c=='z') return 'A'; else if (c>='a'&&c<'z') return Character.toUpperCase((char) (c+1)); else { return Character.toLowerCase((char) (c+1)); } } else if (Character.isDigit(c)){ if (c=='9') return '0'; else return (char) (c+1); } return c; } public static char change2(char c) { if (Character.isLetter(c)) { if (c=='A') return 'z'; else if (c=='a') return 'Z'; else if (c>'a'&&c<='z') return Character.toUpperCase((char) (c-1)); else { return Character.toLowerCase((char) (c-1)); } } else if (Character.isDigit(c)){ if (c=='0') return '9'; else return (char) (c-1); } return c; } }