您的位置 首页 java

简单随机生成6位验证码之Java实现

demo 需求:

随机生成6位验证码,由26个大写字母和0-9数字混搭,其中每位出现数字或字母的概率必须一样并输出到控制台 

demo主要技术:

new Random().nextInt(n)的运用,该方法返回0-n(前包后不包)的随机数 

demo主要代码展示:

import java.util.Random;

/**

* 生成6位验证码

* @author 沐兮沐楚

*

*/

public class Testshow {

private static String[] alphabet = { “A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”,

“Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z” };

public static void main(String[] args) {

System.out.println(getVerCode());

}

public static String getVerCode(){

StringBuffer code = new StringBuffer();

Random random = new Random();

int temp;

for ( int i = 0; i < 6; i++){

if (random.nextInt(2) == 0){

temp = random.nextInt(10);

code. append (temp);

} else {

temp = random.nextInt(26);

code.append(alphabet[temp]);

}

}

return code.toString();

}

}

文章来源:智云一二三科技

文章标题:简单随机生成6位验证码之Java实现

文章地址:https://www.zhihuclub.com/187044.shtml

关于作者: 智云科技

热门文章

网站地图