zoukankan      html  css  js  c++  java
  • 【0730拓展作业】随机生成6位的字符串验证码

    需求:随机生成6位的字符串验证码,要求包含数字、大小写字母 

     1 package com.random;
     2 
     3 public class VerifyCode {
     4     public static String getCode(int length) {
     5         String code = "";
     6         for (int i = 0; i < length; i++) {
     7             boolean boo = (int) (Math.random() * 2) == 0;
     8             if (boo) {
     9                 code += String.valueOf((int) (Math.random() * 10));
    10             } else {
    11                 int temp = (int) (Math.random() * 2) == 0 ? 65 : 97;
    12                 char ch = (char) (Math.random() * 26 + temp);
    13                 code += String.valueOf(ch);
    14             }
    15         }
    16         return code;
    17     }
    18 
    19     public static void main(String[] args) {
    20 
    21         System.out.println(VerifyCode.getCode(6));
    22         System.out.println("-----------------");
    23         System.out.println(VerifyCode.getVerify(6));
    24     }
    25 
    26     public static String getVerify(int length) {
    27         String code = "";
    28         String str = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASFGHJKLZXCVBNM";
    29         String[] strs = str.split("");
    30         for (int i = 0; i < length; i++) {
    31             code += strs[(int) (Math.random() * strs.length)];
    32         }
    33         return code;
    34     }
    35 }
     1 package com.random;
     2 
     3 
     4 import java.util.ArrayList;
     5 import java.util.Collections;
     6 import java.util.List;
     7 import java.util.Random;
     8 
     9 public class Main {
    10       public static void main(String[] args) {
    11             //创建一个ArrayList集合,存放生成的代码
    12             List<Object> list = new ArrayList();
    13             //for循环生成6位验证码
    14             for(int i = 0;i<6;i++) {
    15                 //if语句交替判断生成数字或者字母
    16                 if(i%2==0) {
    17                     int b = (int)(Math.random()*10);//生成0~9的数字
    18                     list.add(b);
    19                 }else {
    20                     boolean flag = false;
    21                     do {
    22                         int a = (int)(Math.random()*58+65);//生成大小写字母的ASCII码
    23                         //if排斥多余的中间其他字符
    24                         if(!(a>=91&&a<=95)) {
    25                             String s = (char)a+"";//数字转字母
    26                             list.add(s);
    27                             flag = true;
    28                         }
    29                     }while(!flag);
    30                 }
    31             }
    32              Collections.shuffle(list);//shuffle随机排序
    33              String out = new String();
    34              for(Object s : list) {
    35                  out+=s;
    36              }
    37              System.out.println(out.toString());
    38         }
    39      
    40 }
  • 相关阅读:
    assign()与create()的区别
    ES6对象扩展——部分新的方法和属性
    ES6对象扩展——扩展运算符
    rest operater剩余操作符
    深拷贝和浅拷贝
    for in和for of的简单区别
    查询ES6兼容的网站
    ES6扩展——对象的扩展(简洁表示法与属性名表达式)
    滚动条设置样式
    marquee横向无缝滚动无js
  • 原文地址:https://www.cnblogs.com/yanglanlan/p/11272398.html
Copyright © 2011-2022 走看看