1. the ancient Roman Emperor Caesar had used the following methods to encrypt military information during the war:
Please write a program, using the above algorithm to encrypt or decrypt the user input of the English string requirements design ideas, program flowchart, source code, results.
Programming Ideas:
Enter the substring to be encrypted str, define the length of the string, convert the string to a single character, each character +3, move backwards by 3, each character 3, move forward 3, define STR1, multibyte the newly obtained word to str1, get the encrypted English substring str1, define STR2, After multibyte each new word to str2, we get the decrypted English substring str2, output the encrypted substring str1, and output the decrypted sub-string str2
Program Flowchart:
Source:
Import Java.util.Scanner;
public class Mi {
public static void Main (string[] args) {
TODO auto-generated method stubs
Scanner input=new Scanner (system.in);
System.out.println ("Please enter a substring to encrypt or decrypt");
String str;
int n=0;
Str=input.nextline ();//nextline accepts one row of data
N=str.length ();
Char A, B;
int n1=0;
String Str1=new string ();
String Str2=new string ();
for (int i=0;i<n;i++)
{
A=str.charat (i);//Gets the string at the specified position
B=str.charat (i);//Gets the string at the specified position
A= (char) (A+3);
B= (char) (b-3);
Str1=str1+a;
Str2=str2+b;
}
SYSTEM.OUT.PRINTLN ("The encrypted substring is: \ n" +str1);
SYSTEM.OUT.PRINTLN ("decrypted substring: \ n" +str2);
}
}
Results:
2. Hands-on brain: Please review the implementation code of the string.equals () method, and learn how to implement it.
The equals () method in the String class is used to compare two strings for equality, because the string is an object type, so it is not easy to use "= =" To determine whether two strings are equal, the entry parameter of the equals () method is the string object to compare, the return value of the method is boolean , and if two strings are equal returns ture, otherwise return false .
3. Organize length () , charat () getchars () replace () Span style= "font-family: the song Body;" >, , touppercase () , , , tochararray () instructions for use .
Length (): Gets the string length, which is an object that contains the length property, which is the long string, using the Length () method to get the property value.
charAt (): gets the character at the specified position
getChars (): gets the substring from the specified position copied into the character array (it has four parameters, 1. The starting position of the copied character in the string 2. The last character copied is added 1 3 to the subscript in the string. Target character Array 4. The copied character is placed in the character array starting subscript)
replace (): substring substitution, with the replace () method of the string class , you can replace a character in the original string with the specified character and get a new string. The method is specifically defined as follows:publicString replace (char Oldchar,char Newchar)
toUpperCase (), tolowercase (): case conversion, in the String class provides two methods to implement the letter case conversion, Their return values are converted strings, where tolowercase () is used to change all uppercase letters in a string to lowercase letters, method touppercase () used to change all lowercase letters in a string to uppercase.
Trim () : Remove the leading and trailing spaces, through the trim () method of the string class , you can get a new string by removing the string's leading and trailing spaces. The method is specifically defined as follows:publicString trim()
ToCharArray () : Converts a String object to a character array
Java string after class job