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