随机产生一个密码,要求同时包含大小写以及数字这三种字符。

  1. public class RandomPasswd {  
  2.     public static String randomPasswd(int n){  
  3.         if(n<3){  
  4.             return null;  
  5.         }  
  6.         //先把前三位分别给一个小写,一个大写,一个数字,这样后面的就可以完全随机,不用担心没有大小写或者数字了  
  7.         char[]passwd=new char[n];  
  8.         int ran=(int)(Math.random()*26);  
  9.         passwd[0]= (char) ('A'+ran);  
  10.         ran=(int)(Math.random()*26);  
  11.         passwd[1]=(char) ('a'+ran);  
  12.         ran=(int)(Math.random()*10);  
  13.         passwd[2]=(char) ('0'+ran);  
  14.         for(int i=3;i<n;i++){  
  15.             //设置一个随机变量,当随机数等于0时,产生小写,等于1时,产生大写,,  
  16.             int rand=(int)(Math.random()*3);  
  17.             if(rand==0){  
  18.                 ran=(int)(Math.random()*26);  
  19.                 passwd[i]= (char) ('A'+ran);  
  20.             }  
  21.             if(rand==1){  
  22.                 ran=(int)(Math.random()*26);  
  23.                 passwd[i]= (char) ('a'+ran);  
  24.             }  
  25.             if(rand==2){  
  26.                 ran=(int)(Math.random()*10);  
  27.                 passwd[i]= (char) ('0'+ran);  
  28.             }  
  29.         }  
  30.         String randompwd= new String(passwd);  
  31.         return randompwd;  
  32.     }  
  33.   
  34.     public static void main(String[] args) {  
  35.         System.out.println(randomPasswd(10));  
  36.     }  
  37. http://blog.csdn.net/u014082714/article/details/53162974

转载于:https://www.cnblogs.com/yd150036/p/6210366.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值