//文本加密及解密
//加密规则:除字母外的符号均不做任何处理,而字母均向后移动四位;
其中:w,x,y,z分别变为a,b,c,d;大写也是如此;解密则反过去即可
//主要了解toCharArray方法的使用
import java.util.Scanner;
public class jiajiemi { //应尽量使用英文EncryptionandDecryption
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String str;
int i = 0;
//加密
System.out.println("Please input the string you want to encrypt!");
str = input.nextLine();
char[] StrArray = str.toCharArray();
//由于string类型不允许改动字符串的内容,我们应先把字符串类型通过java中的toCharArray()方法将字符串类型改为
字符数组类型
System.out.println("Now,I will start to encrypt it:");
while(i<StrArray.length){
if((StrArray[i]>='A'&&StrArray[i]<='Z')||(StrArray[i]>='a'&&StrArray[i]<='z'))
{
if((StrArray[i]>='W'&&StrArray[i]<='Z')||StrArray[i]>='w') StrArray[i]-=22;
else StrArray[i]+=4;
}
i++;
}
//解码
System.out.println("Succeed in Encryption!");
System.out.print(StrArray);
System.out.println();
i = 0;
System.out.println("Start in Decryption!");
while(i<StrArray.length){
if((StrArray[i]>='A'&&StrArray[i]<='Z')||(StrArray[i]>='a'&&StrArray[i]<='z'))
{ //应该可以改用ASCII码的形式实现条件判断,读者可自行去试验
if((StrArray[i]<='D')||(StrArray[i]>='a'&&StrArray[i]<='d')) StrArray[i]+=22;
else StrArray[i]-=4;
}
i++;
}
System.out.println("Succeed in Decryption!");
System.out.print(StrArray);
input.close();
//每次创建了一个Scanner类之后,结束程序时均要close;
}
}