封装验证码简单示例

 1 public class AuthCode {
 2     /**
 3      * 封装验证码
 4      */
 5     public static String createCode(int n){
 6         String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
 7         String code = "";
 8         Random r = new Random();
 9         for (int i = 0; i < n; i++) {
10             int index = r.nextInt(chars.length());
11             code += chars.charAt(index);
12         }
13         return code;
14     }
15 }
1 public class StaticAuthCode {
2     public static void main(String[] args) {
3         String code = AuthCode.createCode(6);
4         System.out.println("验证码是:" + code);
5     }
6 }

 

posted @ 2024-01-21 18:12  小※兽  阅读(26)  评论(0)    收藏  举报