String mobileCode = "";
Random random = new Random();
for (int i = 0; i < 8; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字
if ("char".equalsIgnoreCase(charOrNum)) // 字符串
{
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写字母还是小写字母
mobileCode += (char) (choice + random.nextInt(26));
} else if ("num".equalsIgnoreCase(charOrNum)) // 数字
{
mobileCode += String.valueOf(random.nextInt(10));
}
}生成随机的验证码?大小写字母和数字
最新推荐文章于 2021-06-03 16:27:27 发布
本文介绍了一种使用Java生成包含字母和数字的随机验证码的方法。通过随机选择字符类型并生成相应的字符,最终形成一个由大小写字母及数字组成的8位验证码。
2146

被折叠的 条评论
为什么被折叠?



