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.