A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 游呤人 中级黑马   /  2015-7-18 02:51  /  279 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.Scanner;


  2. /*
  3. * 思路:
  4. *         通过Scanner扫描System.in完成键盘录入
  5. *           private  cutNum()方法录入将明文拆分 那么索引由小到大,分别表式每个数的数位的大小
  6. *         如 arr[0]-----个位
  7. *                 arr[2]-------十位
  8. *                 ......
  9. *         題目指出,
  10. *         private invertedOder() 该方法将这个数进行倒序
  11. *                        
  12. *        
  13. *          private jiaMi() 将传入的数组的每一个数加5并模十,再将每个数。arr[0]与arr[Maxindex]交换数据。
  14. *
  15. *         未了保证安全性将这三个方法制定为ptivate的。
  16. *
  17. * */
  18. //@SuppressWarnings("all")
  19. public class JiaMi {
  20.         int text;
  21.         int password;

  22.         /*
  23.          * // text=123456; // arr[0]=6; // arr[1]=5; //
  24.          */
  25.         private int[] cutNum() {
  26.                 int text = this.text;
  27.                 int[] temp = new int[8];
  28.                 int[] arr = null;
  29.                 int cont = 0;
  30.                 while (text > 0) {
  31.                         temp[cont] = text % 10;
  32.                         text /= 10;
  33.                         cont++;
  34.                 }
  35.                 if (cont >= 8) {
  36.                         throw new RuntimeException("超过8位数");
  37.                 }
  38.                 arr = new int[cont];
  39.                 for (int i = 0; i <= cont - 1; i++) {
  40.                         arr[i] = temp[i];
  41.                 }
  42.                 return arr;
  43.         }

  44.         private int[] invertedOder(int[] arr) {
  45.                 int[] invertedArr = new int[arr.length];
  46.                 for (int i = arr.length - 1, j = 0; i >= 0; i--, j++) {
  47.                         invertedArr[j] = arr[i];
  48.                 }
  49.                 return invertedArr;
  50.         }

  51.         private int jiaMi(int[] arr) {
  52.                 int pass = 0;
  53.                 for (int i = 0; i < arr.length; i++) {
  54.                         arr[i] = (arr[i] + 5) % 10;
  55.                 }
  56.                 int tmp = arr[0];
  57.                 arr[0] = arr[arr.length - 1];
  58.                 arr[arr.length - 1] = tmp;
  59.                 pass = arr[arr.length - 1];
  60.                 for (int i = arr.length - 2; i > -1; i--) {
  61.                         pass *= 10;
  62.                         pass += arr[i];
  63.                 }
  64.                 return pass;
  65.         }

  66.         private int jiaMin() {
  67.                 return jiaMi(invertedOder(cutNum()));
  68.         }

  69.         public void setText() {
  70.                 Scanner sc = new Scanner(System.in);
  71.                 this.text = sc.nextInt();
  72.         }

  73.         public int getpass() {
  74.                 password = jiaMin();
  75.                 return password;
  76.         }

  77.         public static void main(String[] args) {
  78.                 JiaMi jm = new JiaMi();
  79.                 jm.setText();
  80.                 System.out.println(jm.getpass());
  81.         }
  82. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马