不知道啥时候起,密保卡开始流行了。前几天没事自己写了一个生成密保卡的程序,赶赶流行。
- import java.util.Random;
- public class MiBaoKa {
- //定义数组
- private String[][] mbk = null;
- //定义列数
- private int row=10;
- //定义行数
- private int col=8;
- /**
- * 生成密保卡序列
- * @return
- */
- public void creatMBK(){
- mbk = new String[row][col];
- //取160个随机数
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col;j++ ) {
- //取随机数
- int index1 = new Random().nextInt(10);
- int index2 = new Random().nextInt(10);
- //保存到字符串
- mbk[i][j]=index1+""+index2;
- }
- }
- }
- /**
- * 取指定位置密保卡数码
- * @param sb
- * @param r
- * @param c
- * @return
- */
- public String getStr(int r,int c){
- return mbk[r][c];
- }
- public int getRow() {
- return row;
- }
- /**
- * 设置行数
- * @param row
- */
- public void setRow(int row) {
- this.row = row;
- }
- public int getCol() {
- return col;
- }
- /**
- * 设置列数
- * @param col
- */
- public void setCol(int col) {
- this.col = col;
- }
- /**
- * 测试程序
- * @param args
- */
- public static void main(String[] args) {
- int row = 10;
- int col = 8;
- MiBaoKa mbk = new MiBaoKa();
- mbk.setRow(row);
- mbk.setCol(col);
- mbk.creatMBK();
- System.out.println("打印密保卡数据:");
- System.out.println("-------------我是牛叉的分割线-------------");
- System.out.println();
- //打印横表头
- String shu = "ABCDEFGHIJKLMNOPLRSTUVWXYZ";
- System.out.print(" ");
- for (int j = 0; j < col; j++) {
- System.out.print(" "+shu.charAt(j));
- }
- System.out.println();
- System.out.print(" +");
- for (int j = 0; j < col; j++) {
- System.out.print(" - ");
- }
- System.out.println();
- //打印全部数据
- for (int i = 0; i < row; i++) {
- //打印竖表头
- System.out.print(i+" | ");
- for (int j = 0; j < col; j++) {
- String str = mbk.getStr(i,j);
- System.out.print(str);
- System.out.print(" ");
- }
- System.out.println();
- }
- System.out.println();
- System.out.println("-------------我是牛叉的分割线-------------");
- System.out.println();
- System.out.println("取随机位置的值:");
- for (int i = 0; i < 3; i++) {
- //取随机位置的值
- int index1 = new Random().nextInt(col);
- int index2 = new Random().nextInt(row);
- String str = mbk.getStr(index2,index1);
- System.out.println(shu.charAt(index1)+""+index2+"的值是:"+ str);
- }
- }
- }
结果:
打印密保卡数据:
-------------我是牛叉的分割线-------------
A B C D E F G H
+ - - - - - - - - - - -
0 | 44 22 13 26 70 73 07 62
1 | 01 23 93 84 59 80 09 73
2 | 84 35 60 87 96 81 69 41
3 | 59 50 11 91 13 90 64 27
4 | 88 69 52 05 94 28 17 17
5 | 14 30 09 08 47 78 95 06
6 | 89 05 96 04 83 34 18 76
7 | 25 81 55 52 23 88 91 25
8 | 21 91 71 77 64 41 68 70
9 | 62 80 71 45 20 35 23 76
-------------我是牛叉的分割线-------------
取随机位置的值:
D1的值是:84
F3的值是:90
D4的值是:05