1 package test_2_6; 2 3 import java.util.Scanner; 4 5 public class Encryption { 6 7 public static void main(String[] args) { 8 9 /** 数据是四位的整数,加密规则如下: 10 * 每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。 */ 11 12 System.out.println("请输入一个四位数:"); 13 Scanner sc = new Scanner(System.in); 14 int num = sc.nextInt(); 15 encryption(num); 16 17 } 18 19 private static void encryption(int num) { 20 21 char[] ch = ("" + num).toCharArray(); 22 23 for (int i = 0; i < ch.length / 2; i++) { 24 char temp = ch[i]; 25 ch[i] = ch[ch.length - i - 1]; 26 ch[ch.length - i - 1] = temp; 27 } 28 29 String str = ""; 30 for (int i = 0; i < ch.length; i++) { 31 str += (ch[i] - ‘0‘ + 5) % 10; 32 } 33 34 System.out.println("加密后为:" + str); 35 } 36 37 }
结果如下:
请输入一个四位数:
3867
加密后为:2138
[20-04-27][Self-test 15]Java Encryption
原文:https://www.cnblogs.com/mirai3usi9/p/12788438.html