Using arrays for analog encryption in Java

Source: Internet
Author: User

A company uses a public telephone to pass data, and the data is a four-bit integer that is encrypted during delivery.

The encryption rule is to add 5 to each digit, and then replace the number by the remainder of 10, and then swap the first and fourth bits, second and third.

Write a program that receives a four-bit integer and prints the number of encrypted output.

This problem originally did not require the use of arrays, so I was the first to use a translation problem method to complete the problem, the code is as follows:

public static void Main (string[] args) {

System.out.println ("Please enter a 4-digit number:"); Scanner SC=NewScanner (system.in); intN=0; intI=0; while(true) {n=Sc.nextint (); if(n<999| | n>10000) {System.out.println ("The number you entered is not legal!" Please re-enter "); }Else{ Break; } } intGewei=n%10; N/=10; intShiwei=n%10; N/=10; intBaiwei=n%10; N/=10; Gewei+=5; Shiwei+=5; Baiwei+=5; N+=5; intA=gewei%10; intB=shiwei%10; intC=baiwei%10; intD=n%10; intout=d*1000+c*100+b*10+A; System.out.println ("The number after encryption is:" +out);
}

After finishing I remember I just learned the array, want to try to use an array of ways to complete the problem, try to implement several times later, the code is as follows:

 Public Static voidMain (string[] args) {System.out.println ("Please enter a 4-digit number:"); Scanner SC=NewScanner (system.in); intN=0; intA=0;  while(true) {n=Sc.nextint (); if(n<999| | n>10000) {System.out.println ("The number you entered is not legal!" Please re-enter "); }Else{ Break;} }        int[] m=New int[4];  for(inti=0;i<m.length;i++) {M[i]=n%10; N/=10;M[i]+=5; M[i]%=10;        System.out.print (M[i]); } }

After completion I found that the problem with the array to complete the core code is actually only a for loop, and the title of the 4-digit this condition can be easily changed to 5-bit 6-bit ... You do not need to change the code too much.

Related Article

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.