题解 | #字符串加解密#
字符串加解密
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 a = in.nextLine(); String b = in.nextLine(); System.out.println(resolvePWd(a, true)); System.out.println(resolvePWd(b, false)); } } public static String resolvePWd(String pwd, boolean code) { StringBuilder res = new StringBuilder(); int digStep = code ? 1 : -1; char[] pwdChars = pwd.toCharArray(); for (int i = 0; i < pwdChars.length; i++) { char cur = pwdChars[i]; if ('A' <= cur && cur <= 'Z') { int dig = cur + 32 + digStep; char ch = (char) (dig); if (dig < 97) { ch = 'z'; } if (dig > 122) { ch = 'a'; } res.append(ch); } else if ('a' <= cur && cur <= 'z') { int dig = cur - 32 + digStep; char ch = (char) dig; if (dig < 65) { ch = 'Z'; } if (dig > 90) { ch = 'A'; } res.append(ch); } else if ('0' <= cur && cur <= '9') { int dig = cur - 48 + digStep; if(dig >= 10){ dig = dig -10; }else if( dig < 0){ dig = dig +10; }else{ dig = dig % 10; } res.append(dig); } else { res.append(cur); } } return res.toString(); } }