java实现随机中文


原文:http://blog.csdn.net/u013926110/article/details/44600601

 

 

public class CreateCheckCode {
    
    /**
     * 生成随机汉字
     * @return
     */
    public static char getRandomChar() {
        String str = "";
        int hightPos;
        int lowPos;

        Random random = new Random();

        hightPos = (176 + Math.abs(random.nextInt(39)));
        lowPos = (161 + Math.abs(random.nextInt(93)));

        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(hightPos)).byteValue();
        b[1] = (Integer.valueOf(lowPos)).byteValue();

        try {
            str = new String(b, "GB2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return str.charAt(0);
    }
    
    public static void main(String[] args) {
        
        char[] words = new char[4];
    
        for (int i = 0; i<words.length; i++) {
            words[i] = getRandomChar();
        }
        
        System.out.println(words);
    }

}

 

http://blog.csdn.net/u013926110/article/details/44600601


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM