Joke Collection Website - Public benefit messages - Generate a 6-digit verification code with java, which consists of uppercase and lowercase letters and cannot repeat characters.

Generate a 6-digit verification code with java, which consists of uppercase and lowercase letters and cannot repeat characters.

The Random class can be used to generate random, but it cannot be repeated. You can put it in HashSet, because the value of HashSet cannot be repeated. See the following code:

Public? Class? GenerateRandomLetterAndNum? {

Public? Static electricity Invalid? main(String[]? args)? {

? Set< string & gt? Shop? =? getletterandnum(6);

?

? PrintSet (shop);

}

Public? Static electricity Set< string & gt? getletterandnum(int? Length)? {

Set< string & gt? Settings? = new? HashSet & lt string & gt ();

For what? (int? Me? =? 0; ? Me? & lt? Length; ? i++)? {

String? Value? =? get random();

Set.add (value);

}

What if? (set . size()& lt; Length)? {? //If 6 bits are not generated

String? Value? =? get random(); //Continue to call the method of generating random numbers.

Set.add (value);

}

Return? Settings;

}

Private? Static electricity String? getrandom()? {? //method for generate random letters and number

String? Value? =? "";

Random? Random? =? New? random();

int? gen? =? random . nextint(2); //0、 1、2

String? Charonam? =? gen? %? 2? ==? 0“char”? :? "Num";

What if? (“char”。 Is equal to (charornum)? {

int? Temporary workers? =? random.nextInt(2)? %? 2? ==? 065? :? 97;

int? ascii? =? random . nextint(26);

Value? +=? (char)? (ascii? +? temp);

}? Or what? What if? (“num”。 equalsIgnoreCase(charornum))? {

Value? +=? string . value of(random . nextint( 10));

}

Return? Value;

}

Public? Static electricity Invalid? printSet(Set? set){? //Method of printing settings

? Iterator? Iterator? =? set . iterator();

? What time? (iterator.hasNext())? {

String? ele? =? (string)? iterator . next();

System.out.print(ele+"?" );

}

}