最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

Java 开发验证码。随机产生一个四位数的验证码,每位数可能是数字、大写字母或小写字母。

IT圈 admin 6浏览 0评论

Java 开发验证码。随机产生一个四位数的验证码,每位数可能是数字、大写字母或小写字母。


package Test;import java.util.Random;public class CVC {public static void main(String[] args) {String code = CAPTCHA(4);//生成四位数的随机数System.out.println("随机验证码:" + code);}private static String CAPTCHA(int n) {Random r = new Random();String code = " ";//分配一个空字符内存for(int i = 0; i < n; i++) {int type = r.nextInt(3);switch(type) {case 0://大写字母char c0 = (char)(r.nextInt(26) + 65);//ASII中大写字母的范围code += c0;break;case 1 ://小写字母char c1 = (char)(r.nextInt(26) + 97);//ASII中小写字母的范围code += c1;break;case 2://数字int m = r.nextInt(10);//生成0~9的随机数code += m;break;}}return code;}
}

输出

随机验证码: 6qsw

Java 开发验证码。随机产生一个四位数的验证码,每位数可能是数字、大写字母或小写字母。


package Test;import java.util.Random;public class CVC {public static void main(String[] args) {String code = CAPTCHA(4);//生成四位数的随机数System.out.println("随机验证码:" + code);}private static String CAPTCHA(int n) {Random r = new Random();String code = " ";//分配一个空字符内存for(int i = 0; i < n; i++) {int type = r.nextInt(3);switch(type) {case 0://大写字母char c0 = (char)(r.nextInt(26) + 65);//ASII中大写字母的范围code += c0;break;case 1 ://小写字母char c1 = (char)(r.nextInt(26) + 97);//ASII中小写字母的范围code += c1;break;case 2://数字int m = r.nextInt(10);//生成0~9的随机数code += m;break;}}return code;}
}

输出

随机验证码: 6qsw

发布评论

评论列表 (0)

  1. 暂无评论