猿问

刚学java不久,试着写了个最基础简单的代码,高手大神们,指导指导 *-*

  写了个很简单的验证码的代码,高手大神们看看,指点指点  *-* 谢谢

public static void main(String[] args) {
        //生成验证码(4位)
        char[] codes={'3','4','5','6','7','8','a','b','c',
                'd','e','f','h','i','j','k','m',
                'n','p','q','r','s','t','u','v',
                'w','x','y','z','A','B','C','D','E','F',
                'G','H','I','J','K','M','N','P','Q','R',
                'S','T','U','V','W','X','Y'};
        int n=4;
        char[] ver=new char[n];
        
        int index=0;
        int i;
        Random random=new Random();
        boolean[] used=new boolean[codes.length];
        do{
            i=random.nextInt(codes.length);
            if(used[i]){
                continue;
            }
            ver[index++]=codes[i];
            used[i]=true;
        }while(index!=ver.length);
        System.out.println(ver[0]+" "+ver[1]
                +" "+ver[2]+" "+ver[3]);
        
        //获取用户输入
        Scanner console=new Scanner(System.in);
        String input;
        do{
            System.out.print("请输入验证码:");
            input=console.nextLine();
            input=input.trim();
        }while(input.length()!=ver.length);
        char[] inp=input.toCharArray();
        //System.out.println(inp);
        
        //设置忽略大小写
        //char ch;
        for(i=0;i<ver.length;i++){
            if(inp[i]-ver[i]=='A'-'a' || 
                    inp[i]-ver[i]=='a'-'A'){
                inp[i]=ver[i];
            }
        }
        System.out.println(
                Arrays.toString(ver));
        System.out.println(
                Arrays.toString(inp));
        String in=new String(inp);
        String check=new String(ver);
        if(in.equals(check)){
            System.out.println("OK!");
        }else{
            System.out.println("输入错误!");
        }
    }
}
动漫人物
浏览 504回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答