生成一个五位的随机验证码

    xiaoxiao2022-06-24  193

    import java.util.Random; /** * 验证码 * @author Administrator * */ public class CheckCode { public static void main(String[] args) { System.out.println(RandomCheckCode.getCode()); } } class RandomCheckCode{ //生成五位验证码 public static String getCode(){ char []cs = {'A','B','C','D','E','F','G','1','2','3','4','5','6','7','8','9','0'}; //下标 int count = 0; //随机数 Random random = new Random(); //动态的字符数组:用数组存储 然后遍历 StringBuilder sb = new StringBuilder(); while (true) { char c = cs[random.nextInt(cs.length)]; if (sb.indexOf(c+"")==-1) { sb.append(c); count++; if (count==5) { break; } } } return sb.toString(); } }

    最新回复(0)