1.不用startsWith和endsWith的方法,判断开头结束的字符
String str = "adbckfnl"; System.out.println("字符串为 "+str); Scanner sc =new Scanner(System.in); System.out.print("请输入开头字符"); String a=sc.nextLine(); String starts =str.substring(0, 2); if(a.equals(starts)==true) { System.out.println("您输入的是开头字符 "+ " "+starts); } else { System.out.println("您输入的不是开头字符"); } System.out.print("请输入结束字符"); String b=sc.nextLine(); String ends =str.substring(7,8); if(b.equals(ends)==true) { System.out.println("您输入的是结束字符" +" "+ends); } else { System.out.println("您输入的不是结束字符"); }
2.随机生成四位验证码,随机生成5次
String str1 ="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ"; char[] array = new char[4]; Random random = new Random(); System.out.print("验证码为:"); for(int i =0; i<4; i++) { array[i]=str1.charAt(random.nextInt(61)); } System.out.println(array); Scanner cc =new Scanner(System.in); System.out.println("请输入验证码:"); String zh =new String(array); for(int j=0;j<5;j++) { if(cc.nextLine().equals(zh)) { System.out.println("输入正确"); break; } else { if(j<4){ System.out.println("输入错误,请重新输入"); System.out.print("验证码为:"); for(int i =0; i<4; i++) { array[i]=str1.charAt(random.nextInt(61)); } System.out.println(array); zh=new String(array); System.out.println("请输入验证码:");} else { System.out.println("对不起,您已经不能输入了"); } } }