The random algorithm implemented in the Random class is pseudo-random, that is, regular randomness. When performing randomness, the origin number of the random algorithm is called the seed number, and a certain transformation is made on the basis of the seed number to produce the required random number.
- import java.util.Random;
- import java.util.Scanner;
- public class yzm {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- char[] arr={
- '0','1','2','3','4','5','6','7','8','9',
- 'a','b','c','d','e','f','g','h','i','j',
- 'k','l','m','n','o','p','q','r','s','t',
- 'v','w','x','y','z'
- };
- Random rd=new Random();
-
- String str="";
- for(int i=0;i<4;i++){
- int n=rd.nextInt(35);
- str=str+arr[n];
- }
- System.out.println(str);
- System.out.println("请输入验证码:");
- Scanner sn=new Scanner(System.in);
- String sin=sn.next();
- if(str.toLowerCase().equals(sin.toLowerCase())){
- System.out.println("验证码正确!");
- }else{
- System.out.println("验证码错误!");
- }
- //IT论坛 www.itsvse.com
-
- }
- }
Copy code
|