生成6位随机数(带英文)

package test;

import java.util.*;

public class  randomNumUtils{
    /*
    程序实现的是打印出6位验证码有字符有数字交替出现
    */
     
    public static String randomNum(){
      String  result = "";
      String  result2= "";
      for(int i = 1;i <=6; i++)                  //6次执行输出6个不同字符
            {
            //判断产生的随机数是0还是1,是0进入if语句用于输出数字,是1进入else用于输出字符
               int rd = Math.random() >= 0.5 ? 1 : 0;   
            
                if(rd == 0)
                    {
                       Random r = new Random();      
                       result = r.nextInt(9) +1 + "";      //产生1-9数字
                 
                    }
                else{
                    //'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
                 char[] A_z = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
                 Random sc = new Random(); 
                 int sub = sc.nextInt(A_z.length);
                 result = A_z[sub] + "";      //产生A——z字符
                
                    }
                 result2 +=result;
            }
    return result2;
            
        
    }
    public static void main(String[] args) {
        System.out.println(randomNum());
    }
    
}

猜你喜欢

转载自blog.csdn.net/weixin_39892176/article/details/82811932
今日推荐