验证码终极版

题目:随机生成验证码,然后验证

源代码

package Six_Code;

import java.util.Random;
import java.util.Scanner;

public class Four_Code {

    
    public static String getRandomString(int length)
    { // length 字符串长度
        StringBuffer buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        StringBuffer sb = new StringBuffer();
        Random r = new Random();
        int range = buffer.length();
        for (int i = 0; i < length; i ++) 
        {
            sb.append(buffer.charAt(r.nextInt(range)));
        }
        return sb.toString();
    }
    
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int s = 0;
        while(s == 0)
        {
            String Code = getRandomString(6);
            System.out.println("验证码:"+ Code+ "(区分大小写)");
            System.out.println("请输入验证码:");
            Scanner input = new Scanner(System.in );
            String hedui = input.nextLine();
            if(hedui.contentEquals(Code))
                {
                    System.out.println("验证码正确!");
                    s = 1;
                }    
            else 
            {
                System.out.println("验证码错误!");
                s = 0;
            }
        }
    }

}

 

posted @ 2020-10-05 22:30  居酿圆子  阅读(77)  评论(0编辑  收藏  举报