Question:

古罗马皇帝凯撒在打仗时曾经使用过以下方法加密军事情报:

 

请编写一个程序,使用上述算法加密或解密用户输入的英文字串。

要求设计思想、程序流程图、源代码、结果截图。

 

 

 

Answer:

 

设计思想:先将用户输入的英文字串通过String类里的toCharArray()方法

转换为字符数组,然后依次将数组里的元素都加上3(是ASCII在进行计算),作为密文。需要做进一步

处理的是,最后三个字母X、Y、Z加上3之后,应该再减去26才能对应上密文A、B、C。当然,对于小写字母x、y、z同理。

 

 

程序流程图:

 

源代码:

import java.util.Scanner;
public class StringEncryption
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("请输入任意英文字符串:");

String s=sc.nextLine();

char c[]=s.toCharArray();


for(int i=0;i<s.length();i++)
{
c[i]=(char)(c[i]+3);

if((c[i]>90&&c[i]<97)||c[i]>122) c[i]=(char)(c[i]-26);
}

String result="该英文字串加密后的密文为:\n";
for(int i=0;i<s.length();i++)
{result+=c[i];


}
System.out.println(result);
}
}

 

 

 

 

结果截图:

 

·