- package com.itcast.itheima;
- public class SecureAlgorithm {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- SecureAlgorithm sa = new SecureAlgorithm();
- int[] arr = {1,2,3,4,5,6};
- arr = sa.encrypt(arr);
- sa.printArray(arr);
- }
- //加密算法
- public int[] encrypt(int[] arr){
- int[] newArr = new int[arr.length];
-
- for(int i=arr.length-1,j=0;j<newArr.length;i--,j++){
-
- //让每位数字+5
- newArr[j] = arr[i] + 5;
-
- }
- //求出总数
- int sum = sum(newArr);
- //求出%10余数
- mod(newArr);
-
- return newArr;
- }
- //求模(求余)
- private void mod(int[] arr){
-
- for(int i=0;i<arr.length;i++){
- arr[i] = arr[i]%10;
- }
- }
-
- //换位
- public void swap(int[] arr,int i,int j){
-
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
-
- }
-
- //求最和
- private int sum(int[] arr){
-
- int sum = 0;
-
- for(Integer i : arr){
- sum +=i;
- }
-
- return sum;
- }
- //打印数组
- public void printArray(int[] arr){
- for(Integer i : arr){
- System.out.print(i + ",");
- }
- System.out.println();
- }
- }
复制代码 |