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));
}
}
生成随机的验证码?大小写字母和数字
最新推荐文章于 2023-02-27 21:19:26 发布