package com.zuoye.test; import java.util.Random; import java.util.Scanner; public class 验证码 { public static void main(String[] args) { String a="1234567890qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM"; Random i=new Random(); int[] array=new int[4]; for(int j=0;j<4;j++) { array[j]=i.nextInt(62); System.out.print(a.charAt(array[j])+" "); } System.out.println("请输入上图验证码,注意大小写:"); Scanner c=new Scanner(System.in); String d=c.nextLine(); char[] e={a.charAt(array[0]),a.charAt(array[1]),a.charAt(array[2]),a.charAt(array[3])}; String f=new String(e);//强制转换相同类型,方便比较 boolean g=d.equals(f); for(int z=1;z<6;z++){ if(g==true) { System.out.println("输入正确"); break; } else { System.out.println("输入错误,您还有"+(5-z)+"次机会,请重新输入:"); for(int j=0;j<4;j++) { array[j]=i.nextInt(62); System.out.print(a.charAt(array[j])+" "); } Scanner c1=new Scanner(System.in); String d1=c1.nextLine(); char[] e1={a.charAt(array[0]),a.charAt(array[1]),a.charAt(array[2]),a.charAt(array[3])}; String f1=new String(e);//强制转换相同类型,方便比较 boolean g1=d1.equals(f1); } } } }