A company uses a public telephone to deliver data information, which is an integer less than 8 digits, to ensure security,
Encryption is required during the delivery process, and the encryption rules are as follows:
First, reverse the data, then add 5 to each digit, and replace the number with the remainder divided by 10.
Finally, the first digit and the last digit are exchanged. Give an integer that is less than 8 digits at any given
Then, the encrypted results are printed on the console.
Topic Requirements:
A: The data is an integer less than 8 bits
Defines an int type of data
int number = 123456;
B: Encryption Rules
A: First, reverse the data
Result 654321
B: Then add 5 to each digit and replace the number with the remainder divided by 10.
Result 109876
C: Finally, the first and last digit exchange
Result 609871
C: The results of the encrypted output in the console
With a simple analysis, we know that it would be nice if we had the means to turn this data into an array.
It's not directly written like this:
Int[] arr = {1,2,3,4,5,6};
How do I get the data into a group?
A: Define a data
int number = 123456;
B: Define an array, this time the problem comes, the length of the array is how much?
int[] arr = new Int[8]; No more than 8.
When I assign a value, I use a variable to record the changes in the index.
Defines an index value of 0
int index = 0;
C: Get each data
int GE = number%10
int Shi = number/10%10
int Bai = number/10/10%10
Arr[index] = GE;
index++;
Arr[index] = Shi;
index++;
Arr[index] = Bai;
Source:
import Java.util.Scanner;
Class Jiamimain {public static void main (string[] args) {//Create keyboard Entry Object Scanner sc = new Scanner (system.in);
Please enter a data System.out.println ("Please enter a data (less than 8 bits):");
int number = Sc.nextint ();
Write function to encrypt number//call String result = Jiami (number);
SYSTEM.OUT.PRINTLN ("The result of the encryption is:" + results); * * * Requirements: Write a function, the data number to implement encryption. Two clear: Return value type: String do a concatenation of strings.
Parameter list: int number//public static String Jiami (int number) {//definition array int[] arr = new Int[8];
Definition Index INT index = 0;
Try to put the data in number to the array while (number > 0) {arr[index] = number% 10;
index++;
Number/= 10;
//Add 5 to each data and then 10 to get the remainder for (int x = 0; x < index; x + +) {Arr[x] + + 5;
ARR[X]%= 10;
//The first and last exchange int temp = arr[0];
Arr[0] = arr[index-1];
ARR[INDEX-1] = temp;
Concatenate the elements of an array into a string returns//defines an empty content string s = "";
for (int x = 0; x < index/x + +) {s + = arr[x];
return s; }
}
The above is small series for everyone to bring the Java simple implementation of a string of numbers using the corresponding encryption strategy after the transmission of all the content, I hope to help you, a lot of support cloud Habitat Community ~