java实现的简单密保卡功能
本文概要:使用java实现简单的密保卡功能:获取3个随机密保位,通过密保位获得密码.
这同样是今天笔试遇到的题目,一字没填..悲剧..
1: package org.zzp.test;
2:3: import java.util.Random;
4:5: /**
6: * @author Tony7: * 本类实现动态密保卡功能8: */9: public class CodeCard {10: private String letter = "ABCDEFGH";11: private String[][] code = {
12: { "56", "56", "26", "89", "65", "35", "58", "69" },13: { "59", "36", "22", "05", "59", "20", "56", "26" },14: { "25", "39", "65", "89", "68", "57", "49", "52" },15: { "56", "36", "29", "86", "59", "74", "59", "68" },16: { "12", "30", "50", "26", "98", "20", "65", "20" },17: { "12", "56", "95", "68", "26", "35", "16", "26" },18: { "15", "26", "35", "16", "59", "35", "68", "95" },19: { "11", "65", "98", "56", "26", "35", "26", "68" } };20:21: /**
22: * 输出密保卡矩阵23: */24: public void printCodeCard() {25: System.out.println("-------输出密保卡矩阵---------");
26: for (int i = 0; i < 8; i++) {27: for (int j = 0; j < 8; j++) {28: System.out.print(code[i][j] + " ");
29: }30: System.out.println();31: }32: System.out.println("-----------------------------");
33: }34:35: /**
36: * @return 随机获得的密保卡密保位37: */38: public String getTitle() {
39: String result = "";40: Random rd = new Random();
41: for (int i = 0; i < 3; i++) {42: int x = rd.nextInt(8);
43: int y = rd.nextInt(8);
44: char cx = letter.charAt(x);
45: y++;46: result += cx;47: result += y;48: }49: return result;
50: }51:52: /**
53: * @param title 密保位54: * @return 密码55: */56: public String getCode(String title) {
57: String result = "";58: title = title.toUpperCase();59: int a1 = letter.indexOf(title.charAt(0));
60: int a2 = title.charAt(1) - 48 - 1;
61: int b1 = letter.indexOf(title.charAt(2));
62: int b2 = title.charAt(3) - 48 - 1;
63: int c1 = letter.indexOf(title.charAt(4));
64: int c2 = title.charAt(5) - 48 - 1;
65: String code_a = code[a1][a2];66: String code_b = code[b1][b2];67: String code_c = code[c1][c2];68:69: result = code_a + code_b + code_c;70:71: return result;
72: }73:74: public static void main(String[] args) {75: CodeCard card = new CodeCard();
76: card.printCodeCard();77: String title = card.getTitle();78: String code = card.getCode(title);79: System.out.println("密码位:" + title);
80: System.out.println("密码是:" + code);
81: }82: }
代码质量很差,将就着看看吧.
本文概要:使用java实现简单的密保卡功能:获取3个随机密保位,通过密保位获得密码.
这同样是今天笔试遇到的题目,一字没填..悲剧..
1: package org.zzp.test;
2:3: import java.util.Random;
4:5: /**
6: * @author Tony7: * 本类实现动态密保卡功能8: */9: public class CodeCard {10: private String letter = "ABCDEFGH";11: private String[][] code = {
12: { "56", "56", "26", "89", "65", "35", "58", "69" },13: { "59", "36", "22", "05", "59", "20", "56", "26" },14: { "25", "39", "65", "89", "68", "57", "49", "52" },15: { "56", "36", "29", "86", "59", "74", "59", "68" },16: { "12", "30", "50", "26", "98", "20", "65", "20" },17: { "12", "56", "95", "68", "26", "35", "16", "26" },18: { "15", "26", "35", "16", "59", "35", "68", "95" },19: { "11", "65", "98", "56", "26", "35", "26", "68" } };20:21: /**
22: * 输出密保卡矩阵23: */24: public void printCodeCard() {25: System.out.println("-------输出密保卡矩阵---------");
26: for (int i = 0; i < 8; i++) {27: for (int j = 0; j < 8; j++) {28: System.out.print(code[i][j] + " ");
29: }30: System.out.println();31: }32: System.out.println("-----------------------------");
33: }34:35: /**
36: * @return 随机获得的密保卡密保位37: */38: public String getTitle() {
39: String result = "";40: Random rd = new Random();
41: for (int i = 0; i < 3; i++) {42: int x = rd.nextInt(8);
43: int y = rd.nextInt(8);
44: char cx = letter.charAt(x);
45: y++;46: result += cx;47: result += y;48: }49: return result;
50: }51:52: /**
53: * @param title 密保位54: * @return 密码55: */56: public String getCode(String title) {
57: String result = "";58: title = title.toUpperCase();59: int a1 = letter.indexOf(title.charAt(0));
60: int a2 = title.charAt(1) - 48 - 1;
61: int b1 = letter.indexOf(title.charAt(2));
62: int b2 = title.charAt(3) - 48 - 1;
63: int c1 = letter.indexOf(title.charAt(4));
64: int c2 = title.charAt(5) - 48 - 1;
65: String code_a = code[a1][a2];66: String code_b = code[b1][b2];67: String code_c = code[c1][c2];68:69: result = code_a + code_b + code_c;70:71: return result;
72: }73:74: public static void main(String[] args) {75: CodeCard card = new CodeCard();
76: card.printCodeCard();77: String title = card.getTitle();78: String code = card.getCode(title);79: System.out.println("密码位:" + title);
80: System.out.println("密码是:" + code);
81: }82: }
代码质量很差,将就着看看吧.