简单验证码

public class RundomDemo {

public static void main(String[] args) {

String str="qwertyuiopkjh23gfdsazxc" +
"v456bnmQWERT789YUIOPLKJHG0FDSAZXCVBNM";//0--59
Random run = new Random();
Scanner sc=new Scanner(System.in);


boolean res=true;
while (res) {
System.out.println("请输入验证码[忽略大小写]:");

String code="";

for (int i = 0; i < 4; i++) {
int index= run.nextInt(60);//随机生产0--60[不包含]的一个随机值的整数 0--59
char c=str.charAt(index);//将生成的随机值是作为索引
code+=c;
}

System.out.println("验证码为:"+code);


String nextLine = sc.nextLine();
if (code.equalsIgnoreCase(nextLine)) {//忽略大小写比较
System.out.println("验证码输入正确!");
break;
//res=false;
}
}

}

}

posted @ 2018-05-04 14:57  星星614  阅读(334)  评论(0编辑  收藏  举报