00JAVA语法基础_六位验证码 01
在网上看了许多的源程序,涉及到的东西也不太一样,多了图形处理的,由于还没理解太明白,只是做了控制台。
package Six_Code; import java.util.Random; import java.util.Scanner; public class Four_Code { public static String getRandomString(int length) { // length 字符串长度 StringBuffer buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); StringBuffer sb = new StringBuffer(); Random r = new Random(); int range = buffer.length(); for (int i = 0; i < length; i ++) { sb.append(buffer.charAt(r.nextInt(range))); } return sb.toString(); } public static void main(String[] args) { // TODO Auto-generated method stub int s = 0; while(s == 0) { String Code = getRandomString(6); System.out.println("验证码:"+ Code+ "(区分大小写)"); System.out.println("请输入验证码:"); Scanner input = new Scanner(System.in ); String hedui = input.nextLine(); if(hedui.contentEquals(Code)) { System.out.println("验证码正确!"); s = 1; } else { System.out.println("验证码错误!"); s = 0; } } } }
运行结果: