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.
1. Design Ideas
After entering the data, enter the number n, if n>5, is the encryption process, the data by ASCII code into int type , the converted numbers and numbers are encrypted, and then the data is converted into characters, which is encryption. If n<5, then the decryption process, the data by ASCII code into int type, The converted digital subtraction is the decrypted data, and then the data is converted into characters, which is decryption.
2. Program Flowchart
3. Source Code
Letter 1403-1 Squad Zhang Niu plus decryption
Import Java.util.Scanner;
public class Jjiemi
{
public static void Main (string[] args)
{
TODO auto-generated Method Stub
String input = null;
Scanner sc = new Scanner (system.in);
System.out.println ("Please enter data:");
input = Sc.next ();
StringBuffer changed = new StringBuffer ();
Scanner sc1=new Scanner (system.in);
System.out.println ("Please enter Number n:");
int A;
A=sc1.nextint (); Enter Number n
if (a>5)
{
System.out.println ("Encrypt:");
for (int i = 0;i < Input.length (); i++)
{
char x = Input.charat (i);
if (Input.charat (i) >= ' a ' && input.charat (i) <= ' z ')
{
x = (char) (' a ' + ' Z '-X);
Changed.append (x);
}
else if (x >= ' A ' && x <= ' Z ')
{
x = (char) (' A ' + ' Z '-X);
Changed.append (x);
}
Else
{
Continue
}
}
}
Else
{
System.out.println ("decryption:");
for (int i = 0;i < Input.length (); i++)
{
char x = Input.charat (i);
if (Input.charat (i) >= ' a ' && input.charat (i) <= ' z ')
{
x = (char) (' Z ' + ' a '-X);
Changed.append (x);
}
else if (x >= ' A ' && x <= ' Z ')
{
x = (char) (' Z ' + ' A '-X);
Changed.append (x);
}
Else
{
Continue
}
}
}
SYSTEM.OUT.PRINTLN (changed);
}
}
4. Results
Two String.Equals () Method of hands-on brain
The String.Equals () method compares the string with the specified object. The result is true if and only if the argument is not nullandis a String object that represents the same sequence of characters .
"= =" is to see whether two objects are the same object, that is, two object references are pointing to the same object ( The memory address allocated by Java), of course, if used for int,long,double is not to see whether the two values are equal.
Equals () is a comparison of the contents of two objects, typically if the user does not define the equals () method for his or her own class , then the object is used This method of living the parent class of your class. If your class does not have a field referenced by an object, you do not have to overwrite equals ()(Note that the string field is also an object)string has its own equals () method .
Three Defragment the String class length (), CharAt (), GetChars (), replace (), toUpperCase (), toLowerCase (), Tirm (), ToCharArray () usage notes
1. Length () can be used for string lengths (number of characters), such as Str. Length () to find out str lengths.
2. CharAt () Gets the character Str.charat (0) at the specified position, and gets the 0-bit character of str .
3. GetChars () Gets the substring from the specified position copied to the character array, such as s1.getchars (0,5,chararray,0); the meaning of the four parameters: 1. The starting position of the copied character in the string. 2. The last character copied is added 1 to the subscript in the string. 3. Target character Array. 4. The copied character is placed in the starting subscript of the character array.
4. Replace (), substitution string, replacement (oldstring,newstring); replaces the old string with a new string.
5. toUpperCase (): Converts a string to uppercase, such as str.touppercase(), and converts all lowercase in str to uppercase.
6. toLowerCase (): Converts a string to lowercase, such as str.tolowercase(), and converts all uppercase letters in STR to lowercase.
7. Trim (): Remove leading and trailing blanks, such as str= "abc",str.tirm (), and remove the blanks from Str.
8. ToCharArray () Converts a string object to a character array (requires a new char[] array to save),Str.tochararray(); converted into Char Array.
Hands-on brain and homework