- Java code
import java.util.*;public class Test_YanZheng { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str1="0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; String[] str2=str1.split(","); int sum=1; for(int i=0;i<str2.length;i++) { if(sum%10!=0) System.out.print(str2[i]+" "); else { System.out.print(str2[i]); System.out.println(); } sum++; } System.out.println();//-------------------- //生成验证码 Random rand=new Random(); StringBuilder str=new StringBuilder(); int index=0; System.out.println("验证码为:"); for(int i=0;i<4;i++) { index=rand.nextInt(str2.length)-1; System.out.print(str2[index]); str.append(str2[index]); } System.out.println();//--------------------//检验验证码 System.out.println("请输入:"); Scanner scan=new Scanner(System.in);//格式需记忆 StringBuilder enterStr=new StringBuilder(); enterStr.append(scan.next()); //接受键盘输入的字符串 if(str.equals(enterStr)==true) System.out.println("验证码输入正确"); else System.out.println("验证码输入错误"); }
为什么每次我输入验证码以后都是显示验证码输入错误?
------解决方案--------------------
StringBuilder没有重写equals方法
toString再调用equals