需求:
某系统的数字密码(大于0),比如1983,采用加密方式进行传输。
规则如下:
先得到每位数,然后每位数都加上5 , 再对10求余,最后将所有数字反转,得到一串新数。
import java.util.Scanner;
public class day19 {
public static void main(String[] args) {
//1.数字每位数进行数组存储
//反向推理
Scanner sc=new Scanner(System.in);
//输入需要的数字
int count=0;//位数
int x=sc.nextInt();
int temp=x;
while(x!=0){
x=x/10;
count++;
}
//需要数组动态初始化
int b[]=new int[count];
int i=count-1;
while(temp!=0){
b[i]=temp%10;
temp=temp/10;
i--;
}
//2.进行加密,加上5 , 再对10求余,最后将所有数字反转
//加5 对10求余
for (int j = 0; j < count; j++) {
b[j]=b[j]+5;
}
for (int j = 0; j < count; j++) {
b[j]=b[j]%10;
}
//反转数字
//对数组中数字进行交换得到新数组
for (int j = 0,f=count-1; j <f ;f--, j++) {
int num;
num=b[j];
b[j]=b[f];
b[f]=num;
}
//3.对存储数字数组进行打印
int digit=0;
for (int j = 0; j < count; j++) {
digit=b[j]+digit*10;
}
System.out.println(digit);
}
}