题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
因为用的是傻瓜式加解密方法,不写注释了。
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String s0 = in.nextLine(); String s1 = in.nextLine(); char[] s0c =s0.toCharArray(); char[] s1c = s1.toCharArray(); for(int i=0;i<s0.length();i++){ char ci = s0c[i]; char cij = (char)(ci+1); if(Character.isLowerCase(ci) && ci!='z'){ s0c[i]=String.valueOf(cij).toUpperCase().charAt(0); }else if(ci=='z'){ s0c[i]='A'; }else if(String.valueOf(ci).matches("[A-Y]")){ s0c[i]=String.valueOf(cij).toLowerCase().charAt(0); }else if(ci=='Z'){ s0c[i]='a'; }else if(String.valueOf(ci).matches("[0-8]")){ s0c[i]=cij; }else if(ci=='9'){ s0c[i]='0'; } } for(int j=0;j<s1.length();j++){ char cj = s1c[j]; String cjs = String.valueOf(cj); if(cjs.matches("[B-Z]")){ s1c[j] = (char)(cjs.toLowerCase().charAt(0)-1); }else if(cj=='A'){ s1c[j] = 'z'; }else if(cjs.matches("[b-z]")){ s1c[j] = (char)(cjs.toUpperCase().charAt(0)-1); }else if(cj=='a'){ s1c[j]='Z'; }else if(cjs.matches("[1-9]")){ s1c[j]=(char)(s1c[j]-1); }else if(cj=='0'){ s1c[j]='9'; } } for(char c:s0c) {System.out.printf("%s",String.valueOf(c));} System.out.println(); for(char c:s1c) {System.out.printf("%s",String.valueOf(c));} } } }