题解 | #字符串加解密#
字符串加解密
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); String pas1 = sc.nextLine(); String pas2 = sc.nextLine(); System.out.println(encode(pas1)); System.out.println(decode(pas2)); } public static String encode(String pas) { String res = ""; char[] chs = pas.toCharArray(); for (char c : chs) { if (Character.isLetter(c)) { if (c >= 'a' && c <= 'z') { if (c == 'z') { res += 'A'; } else { res += Character.toUpperCase((char)(c + 1)); } } else { if (c == 'Z') { res += 'a'; } else { res += Character.toLowerCase((char)(c + 1)); } } } if (Character.isDigit(c)) { if (c == '9') { res += 0; } else { res += (char)(c + 1); } } } return res; } public static String decode(String pas) { String res = ""; char[] chs = pas.toCharArray(); for (char c : chs) { if (Character.isLetter(c)) { if (c >= 'a' && c <= 'z') { if (c == 'a') { res += 'Z'; } else { res += Character.toUpperCase((char)(c - 1)); } } else { if (c == 'A') { res += 'z'; } else { res += Character.toLowerCase((char)(c - 1)); } } } if (Character.isDigit(c)) { if (c == '0') { res += 9; } else { res += (char)(c - 1); } } } return res; } }