随机产生验证码并进行输入匹配

》题目要求

  》》用户输入产生验证码的个数

  》》根据个数随机产生验证码

  》》用户输入验证码

  》》验证码比较(不区分大小写)

》程序实现

 1 package cn.fury.test;
 2 
 3 import java.util.Scanner;
 4 /**
 5  * 题目要求:
 6  *         用户输入产生验证码的个数
 7  *         随机产生指定个数的字符
 8  *         提示用户输入验证码
 9  *         进行验证码比较(不区分大小写)然后进行输出
10  */
11 public class Test{
12     public static void main(String[] args) {
13         
14         //字符库
15         char [] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
16                 'o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D',
17                 'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
18                 'U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
19         
20         //输入需要产生的验证码的个数
21         Scanner sc = new Scanner(System.in);    
22         System.out.print("Please input a number to represent the quantity of verification code:");
23         int quantity_of_verification_code = sc.nextInt();
24         sc.nextLine(); //为了解决因为之前输入的换行符导致后面的nextLine()函数失效问题
25         
26         //随机产生指定个数的字符并且进行输出
27         char [] verification_code = new char[quantity_of_verification_code];
28         for(int i = 0; i < quantity_of_verification_code; i++){
29             verification_code[i] = alphabet[(int)(Math.random()*alphabet.length)];
30         }
31         String verification_code_String = new String(verification_code);  
32         System.out.println("The verification code is:" + verification_code_String);
33         
34         //用户输入验证码
35         System.out.print("Please input the verification code:");
36         String input_verification_code = sc.nextLine();
37         
38         //对用户输入的验证码和随机产生的验证码进行比较(不区分大小写),然后进行输出操作
39         if((verification_code_String.toUpperCase()).equals(input_verification_code.toUpperCase())){
40             System.out.println("The verification code you input just now is true.");
41         }else{
42             System.out.println("The verification code you input just now is wrong.");
43         }
44         
45     }
46 }
View Code

》遇到的问题

  1.怎么将字符数组变成一个字符串并且和另外一个字符串进行 + 操作

  2.next() 和 nextLine() 的区别

  3.怎么解决nextLine() 接受之前输入的换行符问题

 1 package atest.dak.com;
 2 
 3 import java.util.Arrays;
 4 import java.util.Scanner;
 5 
 6 /*
 7  * 随机生成指定位数的验证码
 8  * 提醒用户输入
 9  * 判断用户输入是否正确
10  */
11 public class Test{
12     public static void main(String[] args){
13         Scanner scan = new Scanner(System.in);
14         
15         System.out.print("请输入随机产生的验证码的个数:");
16         int number = scan.nextInt();
17         
18         char[] arr_of_random = randome_num(number);  //获取随机验证码
19         System.out.println(Arrays.toString(arr_of_random)); //打印随机验证码
20         
21         System.out.print("请输入验证码:");
22         
23         /*
24          * 注意:由于之前进行输入的时候是用回车作为结束的,所以如果用nextline()的话就会
25          *         将上一次的结束的回车作为这次的输入,又因为nextline()的结束也是回车,所以
26          *         在这里通过nextline()作为输入函数是不会得到任何输入值的;因此我们用next()
27          *         作为这里的输入函数可以有效的避免这种问题。
28          */
29         String answer = scan.next();  //获取输入字符串
30         char[] arr_of_answer = answer.toCharArray(); //将输入字符串转化成字符数组
31         System.out.println(Arrays.toString(arr_of_answer)); //打印输入字符数组
32         
33         int[] result = match(arr_of_answer, arr_of_random);
34         System.out.println(Arrays.toString(result));
35         
36     }
37     
38 //    产生所有小写字母
39     private static void f1(){
40         for(int i = 97; i < 97 + 26; i++){
41             System.out.print("\'"+(char)i+"\'"+",");
42         }
43     }
44     
45 //    产生所有大写字母
46     private static void f2(){
47         for(int i = 65; i < 65 + 26; i++){
48             System.out.print("\'"+(char)i+"\'"+",");
49         }
50     }
51     
52 //    随机产生指定个数的验证码
53     private static char[] randome_num(int figure){
54         char[] cha01 = {'a','b','c','d','e','f','g','h',
55                 'i','j','k','l','m','n','o','p','q','r','s',
56                 't','u','v','w','x','y','z','A','B','C','D',
57                 'E','F','G','H','I','J','K','L','M','N','O',
58                 'P','Q','R','S','T','U','V','W','X','Y','Z'};
59         boolean[] used = new boolean[cha01.length];
60         char[] cha02 = new char[figure];
61         for(int i = 0; i < cha02.length; i++){
62             int j;
63             
64             //进行去重复处理
65             do{
66                 j = (int)(Math.random()*(cha01.length));
67             }while(used[j]);
68             
69             cha02[i] = cha01[j];
70             used[j] = true;
71         }
72         return cha02;
73     }
74 
75 //    对随机验证码和输入的验证码进行匹配
76     private static int[] match(char[] answer, char[] random){
77         int[] result = new int[2];
78         for(int i = 0; i < answer.length; i++){
79             for(int j = 0; j < random.length; j++){
80                 if(random[j] == answer[i]){
81                     result[0]++;
82                     if(j == i){
83                         result[1]++;
84                     }
85                 }
86             }
87         }
88         return result;
89     }
90 }
随机验证码的匹配

 

》问题解决

  三少有点累,今天不想继续写啦,待更新中......

  1.三少暂时还没有想到 -->> 暂时只想到通过Arrays.toString(arr01)来实现,但是可以通过一个自定义方法来实现 2017年3月1日11:27:21

  2.请参见本人博客 -->  next()和nextLine()的区别

  3.方法一:用一个nextLine()来接收那个回车

     方法二:用next()来代替nextLine()

》 程序改进

  》》提醒用户输入产生验证码的个数

  》》提醒用户输入验证码

  》》判断用户的输入是否正确(内容和位置都要进行判断)

  》》如果用户没有完全输入正确,那么输出用户输错的个数

 1 package atest.dak.com;
 2 
 3 import java.util.Arrays;
 4 import java.util.Random;
 5 import java.util.Scanner;
 6 
 7 public class Test01{
 8     public static void main(String[] args) {
 9         
10         /*随机产生指定数量的不重复的验证码*/
11         Scanner scan = new Scanner(System.in);
12         System.out.print("请输入需要产生的验证码的数量:");
13         int number = scan.nextInt();
14         char [] ran_verification_code = create_verification_code(number);
15         System.out.println("随机产生的" + number + "个验证码为: " + Arrays.toString(ran_verification_code));
16         
17         /*输入验证码*/
18         System.out.print("请输入验证码:");
19         String str01 = scan.next();
20         char [] input_verification_code = str01.toCharArray();
21         System.out.println("你输入的验证码是:" + Arrays.toString(input_verification_code));
22         
23         /*验证码判断*/
24         int num_correct = 0;
25         for(int i = 0; i < input_verification_code.length; i++) {
26             for(int j = 0; j < ran_verification_code.length; j++) {
27                 if((input_verification_code[i] == ran_verification_code[j]) && (i == j)) {
28                     num_correct++;
29                 }
30             }
31         }
32         if(number == num_correct) {
33             System.out.println("验证码输入正确。");
34         }
35         else {
36             System.out.println("你输错啦" + (number - num_correct) + "个验证码。");
37         }
38         
39     }
40 
41     /**
42      * 随机产生指定数量的验证码
43      * @param number TODO
44      * @return TODO
45      * 
46      */
47     private static char[] create_verification_code(int number) {
48         char[] letter = create_alphabet();  //存放所有的字母
49         boolean [] judge = new boolean[letter.length]; //存放所有字母是否被选中的状态
50         char[] ran_num = {}; //存放随机产生的验证码
51         int index = 0;
52         Random random = new Random();
53         for(int i = 0; i < number; i++) {
54             do {
55                 index = random.nextInt(letter.length);
56             } while(judge[index]);
57             judge[index] = true; //没选中一个字母,就将这个字母的状态变成选中
58             ran_num = Arrays.copyOf(ran_num, ran_num.length + 1); //扩容处理
59             ran_num[ran_num.length - 1] = letter[index]; //添加元素
60         }
61 //        System.out.println(Arrays.toString(ran_num));
62         return ran_num;
63     }
64 
65     /**
66      * 产生全部大写字母和全部小写字母还有0到9的阿拉伯数字
67      * @return TODO
68      * 
69      */
70     private static char[] create_alphabet() {
71         char x = 'a';
72         char y = 'A';
73         char z = '0';
74         char [] alphabet = {}; //用于存放字符
75         for(int i = 0; i < 26; i++) { //将26个小写字母放到alphabet数组中去
76             alphabet = Arrays.copyOf(alphabet, alphabet.length + 1); //扩容处理
77             alphabet[alphabet.length - 1] = (char)(x + i); //添加元素
78         }
79         for(int i = 0; i < 26; i++) { //将26个大写字母放到alphabet数组中去
80             alphabet = Arrays.copyOf(alphabet, alphabet.length + 1); 
81             alphabet[alphabet.length - 1] = (char)(y + i);
82         }
83         for(int i = 0; i <= 9; i++) { //将10个阿拉伯数字放到alphabet中去
84             alphabet = Arrays.copyOf(alphabet, alphabet.length + 1);
85             alphabet[alphabet.length - 1] = (char)(z + i);
86         }
87 //        System.out.println(Arrays.toString(alphabet));
88         return alphabet;
89     }
90     
91 }
View Code

 

转载于:https://www.cnblogs.com/NeverCtrl-C/p/6105468.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值