Java implementation code that transmits after a string of numbers with corresponding encryption policy

Source: Internet
Author: User

A company uses a public telephone to transmit data, and the data is an integer less than 8 bits, in order to ensure security,
Encryption is required during delivery, and the encryption rules are as follows:
First, the data is reversed, then each digit is added 5, and then divided by the remainder of 10 to replace the number,
Finally, the first and last digits are exchanged. Please arbitrarily give an integer less than 8 bits,
Then, the encrypted results are printed out in the console.

Title Requirements:
A: The data is an integer less than 8 bits
Define an int type of data
int number = 123456;
B: Encryption Rules
A: First, reverse the data
Results 654321
B: Then add 5 to each digit, then replace the number by the remainder of 10.
Results 109876
C: Finally, the first and last digits are exchanged
Results 609871
C: Output The encrypted results in the console

With a simple analysis, we know that it would be nice if we had a way to turn this data into an array.
Not directly written like this:
Int[] arr = {1,2,3,4,5,6};

How to turn data into a group?
A: Define a data
int number = 123456;
B: Define an array, and the question is, what is the length of the array?
int[] arr = new Int[8]; No more than 8.
At the time of assignment, I used a variable to record the changes in the index.
Define an index value of 0
int index = 0;
C: Get every single 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 (Syst em.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 after encryption is:" + result);} /* Requirements: Write a function to encrypt the data number. Two explicit: The return value type: String to make a concatenation of the string. Parameter list: int number */public static String jiami (int #) {///definition array int[] arr = new int[8];//define Index int = 0;//put numbers Try to put it in the array while (number > 0) {arr[index] = number% 10;index++;number/= 10;} Add 5 for each data, and then 10 to get the remainder for (int x = 0; x < index; + +) {Arr[x] + = 5;arr[x]%= 10;} Switch the first and last int temp = arr[0];arr[0] = arr[index-1];arr[index-1] = temp;//to stitch the elements of the array into a string to return//define an empty content string "s =" "; for (int x = 0; x < index; X + +) {s + = arr[x];} return s;}}


Java implementation code that transmits after a string of numbers with corresponding encryption policy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.