【程序48】 题目:某个公司采用公用电话传递数据,数据是四位的整数, 在传递过程中是加密的,加密规则如下:每位数字都加上5, 然后用和除以10的余数代替该数字,再将第一位和第四位交换, 第二位

时间:2022-11-09 12:55:41

/*
2017年3月13日11:46:14
java基础50道经典练习题 例48
Athor: ZJY
Purpose:
【程序48】
题目:某个公司采用公用电话传递数据,数据是四位的整数,
在传递过程中是加密的,加密规则如下:每位数字都加上5,
然后用和除以10的余数代替该数字,再将第一位和第四位交换,
第二位和第三位交换。

*/
import java.util.Scanner;

public class ProgramNo48_1
{
public static void main(String[] args) {
System.out.print("请输入一个四位数: ");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
sc.close();

if((999 < number)&&(10000 > number)) {
System.out.print("加密后的值为: "+encrypt(number));
}else {
System.out.print("输入的不是四位数!");
}
}
private static int encrypt(int number) {
int number_buff = number;
int thousandBit = number_buff/1000;
int hundredBit = number_buff/100%10;
int tenBit = number_buff/10%10;
int unitBit = number_buff%1000;

thousandBit += 5;
hundredBit += 5;
tenBit += 5;
unitBit += 5;

thousandBit %= 10;
hundredBit %= 10;
tenBit %= 10;
unitBit %= 10;

thousandBit = thousandBit+unitBit;
unitBit = thousandBit-unitBit;
thousandBit = thousandBit-unitBit;

hundredBit = hundredBit+tenBit;
tenBit = hundredBit-tenBit;
hundredBit = hundredBit-tenBit;

number_buff = thousandBit*1000 + hundredBit*100 + tenBit*10 + unitBit;
return number_buff;
}
}

/*
2017年3月13日11:46:14
java基础50道经典练习题 例48
Athor: ZJY
Purpose:
*/
public class ProgramNo48_2
{
public static void main(String[] args){
int n = 1234;
int[] a = new int[4];
for(int i=3; i>=0; i--){
a[i] = n%10;
n /= 10;
}
for(int i=0;i<4;i++)
System.out.print(a[i]);
System.out.println();
for(int i=0; i<a.length; i++){
a[i] += 5;
a[i] %= 10;
}
int temp1 = a[0];
a[0] = a[3];
a[3] = temp1;
int temp2 = a[1];
a[1] = a[2];
a[2] = temp2;
for(int i=0; i<a.length; i++)
System.out.print(a[i]);
}
}