Tool: obtain a combination of random letters and numbers (combination of letters and numbers, combination of letters and numbers), tool-type letters
Package util; import java. util. random;/***** @ author jkfeng * Get a combination of Random letters and numbers (combination of letters and numbers, combination of letters and numbers) **/public class RandomCharOrNumUtil {public static void main (String [] args) {System. out. println (getCharAndNum (6); System. out. println (getChar (6); System. out. println (getNum (6 ));} /*** get the combination of random denominator and Number * @ param length * @ return the combination of random letters and numbers */public static String getCharAndNum (int length) {String val = ""; Random r Andom = new Random (); String charOrNum = ""; for (int I = 0; I <length; I ++) {if (I = 0) {charOrNum = "char";} else if (I = (length-1) {charOrNum = "num";} else {charOrNum = (random. nextInt (2) % 2 = 0 )? "Char": "num";} if ("num ". equalsIgnoreCase (charOrNum) {// how to obtain the number int num = random between 0-9. nextInt (10); if (num = 0) {num = 2;} if (num = 1) {num = 5;} val + = String. valueOf (num);} else {// how to obtain random characters // 97-122 0-25char char1 = (char) (97 + random. nextInt (26); if (char1 = 'O') {char1 = 'M';} val + = String. valueOf (char1) ;}} val = val. toUpperCase (); return val;}/*** get a random letter combination * @ param length * @ return random letter combination */public static String getChar (int length) {String val = ""; Random random = new Random (); for (int I = 0; I <length; I ++) {// how to obtain random characters // 97-122 0-25char char1 = (char) (97 + random. nextInt (26); val + = String. valueOf (char1);} val = val. toUpperCase (); return val;}/*** obtain a random number combination * @ param length * @ return random number combination */public static String getNum (int length) {String val = ""; Random random = new Random (); for (int I = 0; I <length; I ++) {// how to obtain the number int num = random between 0-9. nextInt (10); val + = String. valueOf (num);} val = val. toUpperCase (); return val ;}}