Java method generates two codes of any length

| - demand explanation

1, any required length codes generated

2, the case comprising code requested alphanumeric

 

| - implementation

Random number mode, respectively, in figures, inside the case letters drawn characters, extraction times controlled by the loop for

 

| - Content Code

. 1  Package com.work.work3;
 2  
. 3  / ** 
. 4  * 9527 :: @auther
 . 5  * @Description: verification code generator
 . 6  * @program: shi_yong
 . 7  * @Create: 2019-07-30 20:45
 . 8   * / 
. 9  public  class Method, {
 10      // use table char generated codes 
. 11      public  static String verCode1 ( int NUM) {
 12 is          String code = "";   // set a variable for receiving the verification code 
13 is          for ( int I = 0; I <NUM; I ++ ) {
 14              //Using a Boolean variable, determining a single numeric codes or letters is 
15              Boolean the Choose = (( int ) (Math.random () 2 *) == 0)? To true : to false ;
 16              IF (the Choose) {
 . 17                  // if choose true, the figure is selected and connected to the individual codes which code 
18 is                  code + = ( int ) (Math.random () * 10); // select a verification code figure is between 0-9 
. 19              } the else {
 20                  // if false choose, select letters do the individual codes and code connected to the inside
 21                  // use char inside the table number, confirmation of this is the use of the English alphabet uppercase or lowercase,
 22                  // 65 uppercase beginning letters, lowercase letters 97 is the beginning 
23 is                  int TEMP = ((int ) (Math.random () 2 *) == 0) 65:? 97 ;
 24                  char CH = ( char ) ((Math.random () * 26 is) + TEMP);
 25                  code + = CH;
 26 is              }
 27          }
 28          // returns a string 
29          return code;
 30      }
 31 is  
32      public  static string verCode2 ( int NUM) {
 33 is          string code = "" ;
 34 is          // uses variable string receiving all 0-9, az, AZ character 
35         = String String "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
 36          // string string array split into 
37 [          String [] = String.split STR ( "" );
 38 is          for ( int I = 0; I <NUM; I ++ ) {
 39              / / array inside the subscripts a random string 
40              code + STR = [( int ) (Math.random () * str.length)];
 41 is          }
 42 is          return code;
 43 is      }
 44 is  
45      public  static  void main (string [ ] args) {
 46 is         System.out.println ( "Control Method Table:" + Method.verCode1 (. 6 ));
 47          System.out.println ( "Method Split split the string:" + Method.verCode2 (. 6 ));
 48  
49      }
 50 }
Random methods and procedures inlet

 

| - operating results

 

Guess you like

Origin www.cnblogs.com/twuxian/p/11272810.html