1. Design Ideas
First enter a string as prompted
Use Charat () to break down each character of a string, convert it to an int type and add 3, and then convert to type int minus 3 and then to char type
New defines a string variable that links the converted char to the output
2. Flowchart
3. Source code
Package test;
Import Java.util.Scanner;
Class Cipher
{
Scanner input=new Scanner (system.in);
void Incipher ()
{
System.out.println ("Please enter the string you want to encrypt:");
String Str=input.nextline ();
String str2= "";
for (int i=0;i<str.length (); i++)
{
int n= (int) Str.charat (i) +3;
str2=str2+ (char) n;
}
System.out.println ("The string after encryption is:" +STR2);
}
void Outcipher ()
{
System.out.println ("Please enter the string you want to decrypt:");
String Str=input.nextline ();
String str2= "";
for (int i=0;i<str.length (); i++)
{
int n= (int) Str.charat (i)-3;
str2=str2+ (char) n;
}
System.out.println ("Decrypted string is:" +STR2);
}
}
public class Classtest5
{
public static void Main (string[] args)
{
Cipher c=new Cipher ();
Scanner input=new Scanner (system.in);
int choice;
Do
{
System.out.println ("Select if you want to encrypt or decrypt: 1. Encrypt 2. Decrypt");
int Choise=input.nextint ();
if (choise==1)
{
C.incipher ();
}
else if (choise==2)
{
C.outcipher ();
}
Else
SYSTEM.OUT.PRINTLN ("Input Error! ");
System.out.println ("Do you want to continue?" 1. Yes 2. No ");
Choice=input.nextint ();
}while (choice==1);
}
}
4.
Java Classroom assignment Encryption and decryption