小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多