题目:
题目描述:
1、对输入的字符串进行加解密,并输出。
2加密方法为:
当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;
当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;
其他字符不做变化。
3、解密方法为加密的逆过程。输入输出描述:
输入一串要加密的密码 输入一串加过密的密码 输出加密后的字符 输出解密后的字符
思路:预先定义好字母和数组的对应,用两个字符串来对应即可,这个思路好像是我之前某篇博客有提到的。然后就无脑变化输出结果了。
代码:
import java.util.*;
public class Main{
String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
String str2 = "bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA1234567890";
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
String value1 = scan.nextLine();
String value2 = scan.nextLine();
char[] result1 = new char[value1.length()];
char[] result2 = new char[value2.length()];
Main test = new Main();
test.Encrypt(value1.toCharArray(),result1);
test.unEncrypt(value2.toCharArray(),result2);
}
}
//解密
int unEncrypt (char result[], char password[]){
for(int i = 0; i < result.length; i++){
int index = str2.indexOf(result[i]);
if(index != -1){
password[i] = str1.charAt(index);
}else{
password[i] = result[i];
}
System.out.print(password[i]);
}
System.out.println();
return 0;
}
//加密
void Encrypt (char aucPassword[], char aucResult[]){
for(int i = 0; i < aucPassword.length; i++){
int index = str1.indexOf(aucPassword[i]);
if(index != -1){
aucResult[i] = str2.charAt(index);
}else{
aucResult[i] = aucPassword[i];
}
System.out.print(aucResult[i]);
}
System.out.println();
}
}