课后作业
![](https://images2015.cnblogs.com/blog/812000/201510/812000-20151025161045567-471230590.png)
//编写一个加密和解密程序 每个字母往后顺延3个位置
//李国帅 2015.10.25
import java.util.Scanner;
public class PassWord {
public static void main(String[] args){
System.out.println("加密请选1,解密请选2:");
String jiami,jiemi;
Scanner scan;
int i,j;
scan=new Scanner(System.in);
i = scan.nextInt();
if(i==1){
scan=new Scanner(System.in);
System.out.println("请输入要加密的字符:");
jiami=scan.nextLine();
char array[]=jiami.toCharArray();
for(j=0;j<jiami.length();j++){
if(array[j]>=65&&array[j]<=87||array[j]>=97&&array[j]<=119)
array[j]=(char) (array[j]+3);
else if((array[j]>=88&&array[j]<=90)||(array[j]>=120&&array[j]<=122))
array[j]=(char) (array[j]-23);
}
StringBuffer arr = new StringBuffer();
for(j=0;j<jiami.length();j++){
arr.append(array[j]);
}
String newjiami=arr.toString();
System.out.println("加密后的字符为:"+newjiami);
}
else
{
scan=new Scanner(System.in);
System.out.println("请输入密码:");
jiemi=scan.nextLine();
char array[]=jiemi.toCharArray();
for(j=0;j<jiemi.length();j++){
if(array[j]>=65&&array[j]<=67||array[j]>=97&&array[j]<=99){
array[j]=(char)(array[j]+23);
}
else if(array[j]>=68&&array[j]<=90||array[j]>=100&&array[j]<=122){
array[j]=(char)(array[j]-3);
}
}
StringBuffer arr = new StringBuffer();
for(j=0;j<jiemi.length();j++){
arr.append(array[j]);
}
String newjiemi=arr.toString();
System.out.println("解密后的字符为:"+newjiemi);
}
}
}
![](https://images2015.cnblogs.com/blog/812000/201510/812000-20151025165238630-1292883798.jpg)
![](https://images2015.cnblogs.com/blog/812000/201510/812000-20151025165300864-1742454489.jpg)