package com.itheima.anli; import java.util.Random; public class anli2 { public static void main(String[] args) { System.out.println(createCode(4)); } public static String createCode(int n){ Random random=new Random(); String code=""; for (int i = 1; i <= n; i++) { int type=random.nextInt(3);//产生0 1 2之间的数字 switch(type){ case 0: code +=random.nextInt(10); break; case 1: //A:65 Z:65+25 char a=(char)(random.nextInt(26)+65); code+=a; break; case 2: //a:97 z:97+25 char b=(char)(random.nextInt(97)+26); code+=b; break; } } return code; } }
标签:JAVA,String,int,Random,验证码,案例,static,public From: https://www.cnblogs.com/pengsuoqun123/p/18050645