本文基础到优化仅针对验证码中连接各字符的方式和random语句的修改!
目录:
本文写了一个Java随机验证码的一个程序,从基础思想到优化的变化解释,基础和优化都会有具体的解释。验证码在生活中出现频繁例如在登录、注册、支付等各种场景都会使用到验证码,验证码是为了提高安全和有效防机器人而产生的,所以验证码的存在必不可少。本程序需要用到两个开发工具分别是JDK和IDEA。
目标:
使用Java制作一个可以产生随机n位混合验证码的程序。该程序会将数字、大写字母和小写字母混合使用。
核心逻辑:
1.验证码的组成类型:小写字母、大写字母和数字三种类型;
2. 生成随机n位验证码:使用random语句生成随机数,随机产生;
3.组成验证码:使用switch循环和for循环,重复n次循环组合得到n位验证码;
注:生成随机数中的调用包(java.util.Random)在使用IDEA时不需要特别输入,仅输入其指定语句,Idea会帮助你补充。
具体分析:
1.基础的程序分析
(1).创建一个有参方法,可以根据自身所需定义n(验证码位数,我这里取值为6)的取值,并调用;
public static void main(String[] args) {
System.out.println(getCode(6));
}
// 1.定义一个方法,返回一个n位随机数
public static String getCode(int n){}
(2).定义一个变量code用来存储验证码,变量开始为空;
String code = "";
(3).使用for循环,循环n次,生成验证码,为了实现该循环,还需要有一下内容:
1.定义一个type变量,用来装小写字母、大写字母和数字三种类型,我分别定义为0、1、2,但是需要随机在这在这三种类型中选出一个类型,则需要Random方法,因为该语句只输出[0,1)中的所有小数,而我们需要0、1、2这些整数,所以我们将该语句*3,并转成int整数类型。
//3.循环n次,每次生成一个在[0,3)中的随机整数,到switch中寻找对应情况
for (int i = 0; i < n; i++){
int type =(int) (Math.random()*3);//i=0,1,2
2.选择类型有三种情况,在此使用switch语句分别有case 0小写字母、case 1大写字母、case 2数字三种情况,同样需要在每种情况中使用Random语句随机选择一个字符,使用连接符来连接,循环n次得到验证码。
//Switch语句
switch(type) {
case 0:
int lowerIndex=(int) (Math.random()*26);
code += (char)('a'+lowerIndex);
break;
case 1:
int lupperIndex=(int) (Math.random()*26);
code += (char)('A'+upperIndex);
break;
case 2:
int digit=(int) (Math.random()*10);
code += digit;
break;
default:
break;
}
(4).返回验证码。
2.基础的程序完整源代码
package Test;
public class Test2 {
/*随机生成n位数的验证码
* 1.定义一个方法,返回一个n位随机数
* 2.定义一个验证码的字符串,保存随机数
* 3.循环n次,生成一个随机数,并拼接到验证码字符串中
* 4.返回验证码
*/
public static void main(String[] args) {
System.out.println(getCode(6));
}
// 1.定义一个方法,返回一个n位随机数
public static String getCode(int n){
//2.定义一个验证码的字符串,保存随机数
String code = "";
//3.循环n次,生成一个随机数,并拼接到验证码字符串中验证码可能是0-9的数字、大写字母和小写字母
for (int i = 0; i < n; i++){
int type =(int) (Math.random()*3);//i=0,1,2
//Switch语句
switch(type) {
case 0:
int lowerIndex=(int) (Math.random()*26);
code += (char)('a'+lowerIndex);
break;
case 1:
int lupperIndex=(int) (Math.random()*26);
code += (char)('A'+upperIndex);
break;
case 2:
int digit=(int) (Math.random()*10);
code += digit;
break;
default:
break;
}
}
// 4.返回验证码
return code;
}
}
3.优化的程序分析:
1.连接方式的优化:频繁拼接字符串推荐使用 StringBuilder 提升性能。在 Java 中,字符串是不可变对象。这意味着每次拼接字符串时,都会创建一个新的字符串对象,原来的对象不会被修改,会被保留,这会导致无用的字符占用空间并且降低了运行速度。而StringBuilder 是一个可变的字符串类,它允许你在不创建新对象的情况下修改字符串内容,因此使用该方法拼接的效率大大提高。
StringBuilder使用方法:
(1).创建该方法:StringBuilder code = new StringBuilder();
(2).添加字符:code.append(),这是最常用的方法,可以添加字符串、字符、数字等。
(3).转换为 String:code.toString()
StringBuilder code = new StringBuilder();
//Demon:使用code.append()添加
case 0:
char upperIndex=(char)('A'+ra.nextInt(26));
code.append(upperIndex);
break;
//返回值需要返回code的string形式
return code.toString();
2.Random随机语句的优化:
使用 Random 类替代 Math.random()更高效、可读性更好,和基础的用法是一样的。
用法:
(1).创建该方法: Random random = new Random();
(2).随机选择一个小写字符:char lowerIndex= (char)('a'+ra.nextInt(26)),ra.nextInt(26)仅显示[0,26)的所有整数但是加了'a'就会从a到z中随机取一个字母。
注:生成随机数中的调用包(java.util.Random)在使用IDEA时不需要特别输入,仅输入其指定语句,Idea会帮助你补充。
//调包
import java.util.Random;
public class Test2 {
public static String getCode(int n){
Random ra = new Random();
//demon:随机选取一个小写字母
char lowerIndex= (char)('a'+ra.nextInt(26));
}
}
4.优化的程序完整源代码:
package Test;
import java.util.Random;
public class Test2 {
/*随机生成n位数的验证码
* 1.定义一个方法,返回一个n位随机数
* 2.定义一个验证码的字符串,保存随机数
* 3.循环n次,生成一个随机数,并拼接到验证码字符串中
* 4.返回验证码
*/
public static void main(String[] args) {
System.out.println(getCode(6));
}
// 1.定义一个方法,返回一个n位随机数
public static String getCode(int n){
//2.定义一个验证码的字符串,保存随机数
StringBuilder code = new StringBuilder();
Random ra = new Random();
//3.循环n次,生成一个随机数,并拼接到验证码字符串中验证码可能是0-9的数字、大写字母和小写字母
for (int i = 0; i < n; i++){
int type =ra.nextInt(3) ;//i=0,1,2
switch(type) {
case 0:
char upperIndex=(char)('A'+ra.nextInt(26));
code.append(upperIndex);
break;
case 1:
char lowerIndex= (char)('a'+ra.nextInt(26));
code.append(lowerIndex);
break;
case 2:
int digit=ra.nextInt(10);
code.append( digit);
break;
default:
break;
}
}
// 4.返回验证码
return code.toString();}
}