您的位置:首页 > 其它

随机产生字符串

2016-01-29 00:00 651 查看
摘要: 随机产生4位数的字符串,包括数字和字母,作用:用于验证码

package test;

import java.util.Random;

/**
* 产生随机字符串 用途:验证码的产生
*
* @author chenddongj
*
*/
public class RandomStringTest {
private Random random = new Random();
private static char[] captchars = new char[] { 'a', 'b', 'c', 'd', 'e',
'2', '3', '4', '5', '6', '7', '8', 'g', 'f', 'y', 'n', 'm', 'n',
'p', 'w', 'x' };

public void randomStringTest() {
int car = captchars.length - 1;

String randomString = "";
// 产生4个随机字符
for (int i = 0; i < 4; i++) {
randomString += captchars[random.nextInt(car) + 1];

}
System.out.println(randomString);

}

public static void main(String[] args) {
// 调用产生随机数方法
RandomStringTest rt = new RandomStringTest();
rt.randomStringTest();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: