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