? 寫了個很簡單的驗證碼的代碼,高手大神們看看,指點指點 ?*-* 謝謝
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("輸入錯誤!");
}
}
}
添加回答
舉報
0/150
提交
取消