A company uses a public telephone to transmit data information, and the data is an integer less than 8 bits. In order 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 then subdivide the remainder of 10 instead of the number.
The first and last digits are exchanged. Please give any integer less than 8 bits.
Then, print out the encrypted result on the console.
Question Requirements:
A: The data is an integer less than 8 bits
Define an int type data
int number = 123456;
B: Encryption rules
a: First, reverse the data
Results 654321
b: Then add 5 to each digit, and then use the sum divided by the remainder of 10 instead of the number
Results 109876
c: The first and last numbers are exchanged in the end
Results 609871
C: Output the encrypted result on the console
Through simple analysis, we know that it would be great if we had a way to turn this data into an array.
Not written directly like this:
int[] arr = {1,2,3,4,5,6};
How to convert data into an array?
A: Define a data
int number = 123456;
B: Define an array, and the question arises at this time. What is the length of the array?
int[] arr = new int[8]; //It is impossible to exceed 8
When assigning values, I use a variable to record the index changes.
Define an index value of 0
int index = 0;
C: Get every 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 code:
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 after encryption is: " + result);}/* * Requirements: Write a function to encrypt the data number. Two clear: Return value type: String Make a string splicing. Parameter list: int number */public static String jiaMi(int number) {// Define array int[] arr = new int[8];// Define index int index = 0;// Find a way to put the data in number into the array while (number > 0) {arr[index] = number % 10;index++;number /= 10;}// Add 5 for each data, and then get the remainder for 10 (int x = 0; x < index; x++) {arr[x] += 5;arr[x] %= 10;}// Exchange the first and last bits int temp = arr[0];arr[0] = arr[index - 1];arr[index - 1] = temp;// Scatter the elements of the array into a string and return // Define an empty content string String s = "";for (int x = 0; x < index; x++) {s += arr[x];}return s;}}The above is all the contents of Java simple implementation of the transmission of a string of numbers after using the corresponding encryption strategy. I hope it will be helpful to everyone and support Wulin.com more~