The ancient Roman Emperor Caesar used the following methods to encrypt military intelligence during the war: please write a program that uses the above algorithm to encrypt or decrypt the English string entered by the user.
Programming Ideas:
First, the user enters a string that needs to be encrypted, then defines the function to get the string length, converts the string to a single character through A For loop, "+3" for each character, moves backwards by 3, each character "3", moves forward 3, defines str2, and assigned each new word to str2, Get the encrypted English string str2, define the STR3, the newly obtained each word assigned to STR3, get the decrypted English string str3, output the encrypted substring str2, output the decrypted sub-string STR3
Program Source code:
Package Demo;import Java.util.scanner;public class Mi {public static void main (string[] args) {//TODO auto-generated method stub Scanner SC An=new Scanner (system.in); SYSTEM.OUT.PRINTLN ("Telegram requiring encryption is:"); String str;str=scan.nextline ();//nextline function accepts data int length;length=str.length (); String Str1=new string (); String Str2=new string (); String Str3=new string (), char chuan;char qian,hou;for (int i=0;i<length;i++) {str1=str.substring (i,i+1); chuan= Str1.charat (0); qian= (char) (chuan+3); hou= (char) (chuan-3); str2=qian+str2;str3=hou+str3;} SYSTEM.OUT.PRINTLN ("The Encrypted Telegram is:" +STR2); System.out.println ("The Decrypted Telegram is:" +STR3);}}
Program Run Result:
Java Encryption jobs