2016.11.29
When we forget the password or verify the password, we always use the verification code. Then let's take a look at how the verification code is written?
Requirement: Write a function to randomly generate a 4-digit verification code
import java.util.Random; public class Demo5 { public static void main(String[] args) { char[] arr = {'中','country','people','people','wan','years'}; StringBuilder sb = new StringBuilder(); Random random = new Random(); //Need four random numbers, get the characters in the random number through the random number for(int i=0;i<4;i++){ int index = random.nextInt(arr.length);//Generating random numbers must be an array range or something sb.append(arr[index]); } System.out.println("Verification code: "+sb); } }
Doesn't it feel very simple, just a few lines of code can be implemented, you can try it too.