古罗马皇帝凯撒在打仗时曾经使用过以下方法加密军事情报:
请编写一个程序,使用上述算法加密或解密用户输入的英文字串要求设计思想、程序流程图、源代码、结果截图。
设计思想:输入一个字符串,然后将其中每个字符单独取出,并且用字符的算法进行加3,强制转化为后面3位的字符,最后输出。
程序流程图:
源代码:
package test; import java.util.*; //引用util包 public class Caesar { public static void main(String[] args) { // TODO Auto-generated method stub Scanner a = new Scanner(System.in); //初始化Scanner String cipher,result; //创建字符串 System.out.println("输入英文字符串:"); //输出提示字 cipher = a.nextLine();//输入 result = "";//初始化字符串 int i ; for(i=0 ; i<cipher.length();i++){ //循环字符串的长度次 result += (char)(cipher.charAt(i)+3); //把字符串中每个字符取出,并后移,最后连接 } System.out.println("加密后为:"+result);//输出 } }
运行结果:
时间: 2024-12-16 04:16:43