- import java.text.MessageFormat;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- import java.util.Scanner;
- public class Mbk {
- private Map<Integer, Map<Integer, String>> datas = new HashMap<Integer, Map<Integer, String>>();
- private String[] bj = new String[] { "Q", "W", "E", "A", "B", "C", "D",
- "H", "F", "G" };
- private int l = bj.length;
- private int h = 5;
- private int sjl;
- private int sjh;
- private static String sjhmc;
- private static String sjlmc;
- private static String result;
- //初始化一张密保卡
- public void init() {
- //打印标题行
- for (int j = 0; j < l; j++) {
- System.out.print(" "+bj[j]);
- }
- System.out.println();
- //打印密保卡内容信息
- for (int i = 0; i < h; i++) {
- System.out.print(bj[i]+" ");
- Map<Integer,String> tmp=new HashMap<Integer, String>();
- for (int j = 0; j < l; j++) {
- int ys=new Random().nextInt(99);
- if (ys>=10) {
- System.out.print(ys+" ");
- }else{
- System.out.print(ys+" ");
- }
- tmp.put(j,ys+"");
- datas.put(i,tmp);
- }
- System.out.println();
- }
- }
- //随机选择行列
- public void sjxz(){
- sjl=new Random().nextInt(l);
- sjh=new Random().nextInt(h);
- sjl=sjl==0?0:sjl-1;
- sjh=sjh==0?0:sjh-1;
- sjhmc=bj[sjh];
- sjlmc=bj[sjl];
- result=datas.get(sjh).get(sjl);
- }
- public static void main(String[] args) {
- Mbk mdk=new Mbk();
- mdk.init();
- int i;
- for (i=0; i < 2; i++) {
- mdk.sjxz();
- Scanner scan = new Scanner(System.in);
- System.out.println(MessageFormat.format("请输入{0}行{1}列的数字",sjhmc,sjlmc));
- String srz = scan.next();
- if (!result.equals(srz)) {
- break;
- }
- }
- if(i==2)
- System.out.println("验证通过...");
- else
- System.out.println("验证失败...");
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/230920136069.html
来源: http://www.codesnippet.cn/detail/230920136069.html