日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

隨機(jī)生成5位大小寫字母或者數(shù)字

 yan的圖書41 2017-07-17

隨機(jī)生成5位大小寫字母或者數(shù)字

方法一:生成不重復(fù)的

  1. public static void main(String[] args) {  
  2.         Random rand = new Random();  
  3.         char[] letters=new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q',  
  4.                 'R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i',  
  5.                 'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','r',  
  6.                 '0','1','2','3','4','5','6','7','8','9'};  
  7.         String str = "";  
  8.         int index;  
  9.         boolean[] flags = new boolean[letters.length];//默認(rèn)為false  
  10.         for(int i=0;i<5;i++){  
  11.             do{  
  12.                 index = rand.nextInt(letters.length);   
  13.             }while(flags[index]==true);  
  14.             char c = letters[index];  
  15.             str += c;  
  16.             flags[index]=true;  
  17.         }  
  18.         System.out.println(str);  
  19.     }  

方法二:生成重復(fù)的,與方法一類似

  1. public static void main(String[] args) {  
  2.         Random rand = new Random();  
  3.         char[] letters=new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q',  
  4.                 'R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i',  
  5.                 'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','r',  
  6.                 '0','1','2','3','4','5','6','7','8','9'};  
  7.         String str = "";  
  8.         int index;  
  9.         boolean[] flags = new boolean[letters.length];//默認(rèn)為false  
  10.         for(int i=0;i<5;i++){  
  11.             do{  
  12.                 index = rand.nextInt(letters.length);   
  13.             }while(flags[index]==true);  
  14.             char c = letters[index];  
  15.             str += c;  
  16.             flags[index]=true;  
  17.         }  
  18.         System.out.println(str);  
  19.     }  
方法三:生成重復(fù)的(建議選用此方法)
  1. public static void main(String[] args) {  
  2.         String str = "";  
  3.         Random rand = new Random();  
  4.         for(int i=0;i<5;i++){  
  5.             int num = rand.nextInt(3);  
  6.             switch(num){  
  7.                 case 0:  
  8.                     char c1 = (char)(rand.nextInt(26)+'a');//生成隨機(jī)小寫字母   
  9.                     str += c1;  
  10.                     break;  
  11.                 case 1:  
  12.                     char c2 = (char)(rand.nextInt(26)+'A');//生成隨機(jī)大寫字母   
  13.                     str += c2;  
  14.                     break;  
  15.                 case 2:  
  16.                     str += rand.nextInt(10);//生成隨機(jī)數(shù)字  
  17.             }  
  18.         }  
  19.         System.out.println("生成的5個(gè)隨機(jī)驗(yàn)證碼是:"+str);  
  20.     }  




    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多