》题目要求
》》用户输入产生验证码的个数
》》根据个数随机产生验证码
》》用户输入验证码
》》验证码比较(不区分大小写)
》程序实现
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 }