1、LinkedHashSet 存储密码表;不足26位的用A-Z补齐 2、然后遍历要加密的字符串,根据大小写转化为密码表中的字符即可,不要忘记空格;
import java.util.Scanner;
import java.util.LinkedHashSet;
import java.util.ArrayList;
public class Main{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String key = sc.nextLine().toUpperCase();//变为大写
String jiamiString = sc.nextLine();
LinkedHashSet<Character> set = new LinkedHashSet<>();
for(Character c:key.toCharArray()){
set.add(c);
}
int k = 0;
while(set.size()<26){
set.add((char)('A' + k));//密码表补齐
k++;
}
ArrayList<Character> list = new ArrayList<>(set);//密码表
StringBuilder sb = new StringBuilder();
for(Character cc:jiamiString.toCharArray()){
if(Character.isUpperCase(cc)){//大写
sb.append(list.get(cc - 'A'));
}else if(Character.isLowerCase(cc)){//小写
sb.append(Character.toLowerCase(list.get(cc - 'a')));
}else{//空格
sb.append(cc);
}
}
System.out.println(sb.toString());
}
}
}