描述
1、对输入的字符串进行加解密,并输出。
2、加密方法为:
当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;
当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;
其他字符不做变化。
3、解密方法为加密的逆过程。
本题含有多组样例输入。
数据范围:输入的两个字符串长度满足 ,保证输入的字符串都是大小写字母或者数字
输入描述:
输入说明
输入一串要加密的密码
输入一串加过密的密码
输出描述:
输出说明
输出加密后的字符
输出解密后的字符
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] lowerChar = "abcdefghijklmnopqrstuvwxyz".toCharArray();
ArrayList<Character> lowerList = new ArrayList<Character>();
for (Character c : lowerChar) {
lowerList.add(c);
}
char[] upperChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
ArrayList<Character> upperList = new ArrayList<Character>();
for (Character c : upperChar) {
upperList.add(c);
}
char[] mathChar ="0123456789".toCharArray();
ArrayList<Character> mathList = new ArrayList<Character>();
for (Character c : mathChar) {
mathList.add(c);
}
while(sc.hasNext()){
String next = sc.next();
System.out.println(getEncodeStr(lowerList,upperList,mathList,next));
String nextLine = sc.next();
System.out.println(getdecodeStr(lowerList,upperList,mathList,nextLine));
}
}
private static String getEncodeStr(ArrayList<Character> lowerList, ArrayList<Character> upperList, ArrayList<Character> mathList, String next) {
char[] toCharArray = next.toCharArray();
StringBuffer sb = new StringBuffer();
for (Character c:toCharArray) {
if (lowerList.contains(c)) {
if('z'== c ){
c = 'A';
}else{
char charAt = c.toString().toUpperCase().charAt(0);
c = ++charAt;
}
sb.append(c);
continue;
}
if (upperList.contains(c)) {
if('Z'== c){
c='a';
}else{
char charAt = c.toString().toLowerCase().charAt(0);
c = ++charAt;
}
sb.append(c);
continue;
}
if (mathList.contains(c)) {
if (c == '9') {
c = '0';
} else {
int temp = Integer.parseInt(c + "") + 1;
c = (temp + "").charAt(0);
}
sb.append(c);
continue;
}
}
return sb.toString();
}
private static String getdecodeStr(ArrayList<Character> lowerList, ArrayList<Character> upperList, ArrayList<Character> mathList, String nextLine) {
char[] toCharArray = nextLine.toCharArray();
StringBuffer sb = new StringBuffer();
for (Character c:toCharArray) {
if (lowerList.contains(c)) {
if('a'== c ){
c = 'Z';
}else{
char charAt = c.toString().toUpperCase().charAt(0);
c = --charAt;
}
sb.append(c);
continue;
}
if (upperList.contains(c)) {
if('A'== c ){
c = 'z';
}else{
char charAt = c.toString().toLowerCase().charAt(0);
c = --charAt;
}
sb.append(c);
continue;
}
if (mathList.contains(c)) {
if (c == '0') {
c = '9';
} else {
int temp = Integer.parseInt(c + "") - 1;
c = (temp + "").charAt(0);
}
sb.append(c);
continue;
}
}
return sb.toString();
}
}