日期:2014-05-18 浏览次数:20690 次
import java.util.Random; public class Test01 { public static void main(String[] args) { VerifyCode code = new VerifyCode(5); char[] chns = code.getNumName(); char[] chs = code.getRanNums(); System.out.println(chs); System.out.println(chns); code.reset(); chs = code.getRanNums(); chns = code.getNumName(); System.out.println(chs); System.out.println(chns); } } class VerifyCode { private int digit = 0; private char[] chs = null; private char[] chns = null; private final static char[] CHN_NAMES = { '洞', '幺', '两', '叁', '肆', '伍', '陆', '拐', '捌', '勾' }; public VerifyCode(int digit) { this.digit = digit; } public char[] getRanNums() { init(); return chs; } public char[] getNumName() { if(chns == null) { init(); chns = new char[chs.length]; for(int i = 0; i < chs.length; i++) { chns[i] = CHN_NAMES[chs[i] - '0']; } } return chns; } public void reset() { chs = null; chns = null; } private void init() { if(chs != null) { return; } chs = new char[digit]; Random ran = new Random(); for(int i = 0; i < chs.length; i++) { chs[i] = (char)(ran.nextInt(10) + '0'); } } }