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

posted @ 2017-10-15 12:14  这个名字想了很久~  阅读(2256)  评论(0编辑  收藏  举报