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

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



  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值