/*
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]);
}
}