java随机生成中文名字

汉字区位码:
区码范围:01-09区为682个特殊字符,16~87区为汉字区,包含6763个汉字 。其中16-55区为一级汉字(3755个最常用的汉字,按拼音字母的次序排列),56-87区为二级汉字(3008个汉字,按部首次序排列)。
位码范围:区码为55时,位码范围为1-89;其他区码,位码范围为1-94。

随机生成中文名字

import java.io.UnsupportedEncodingException;

public class ChineseName {
    public static void main(String[] args) {
        ChineseName d = new ChineseName();
        System.out.println(d.getName());//输出随机的中文名字
    }

    //获得汉字名字
    public String getName(){
        String name = "";
        int chineseNameNum = (int)(Math.random()*3 + 2);
        for(int i=1; i<=chineseNameNum;i++){
            name += this.getChinese();
        }
        return name;
    }

    //获得单个汉字
    public String getChinese(){
        String chinese = "";
        int i = (int)(Math.random()*40 + 16);
        int j = (int)(Math.random()*94 + 1);
        if(i == 55){
            j = (int)(Math.random()*89 + 1);
        }
        byte[] bytes = {(byte) (i+160),(byte) (j+160)};
        try {
            chinese =  new String(bytes, "gb2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return chinese;
    }
}


将单个汉字转换为区位码:


import java.io.UnsupportedEncodingException;

public class ChineseToCode {
    public static void main(String[] args) {
        ChineseToCode ctc = new ChineseToCode();
        System.out.println(ctc.getCode("王"));
    }

    public String getCode(String chinese){
        byte[] bt = null;
        try {
            bt = chinese.getBytes("gb2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String code = "";
        for(int i = 0;i<bt.length;i++){
            String hex = Integer.toHexString(bt[i] & 0xFF);// 转换为16进制
            if (hex.length() == 1) {
                hex = '0' + hex;                           // 长度是1位的,在前面补0
            }
            hex = hex.toUpperCase();                       // 转换为大写
            int a = Integer.parseInt(hex,16);
            code += (a - 0x80 - 0x20) + "";                // 获得区位码
        }
        return code;
    }
}

代码简单干净,易于扩展。 随机生成用户名样例: 339063 77188 fengjr1980@yeah.net 97133 xupe19870102@sohu.com 18955990722 2145028 25070167 299966 13329809029 18739552501 77223 725525865 cheng1991 chub1974 13290062609 wangf19750725 caot19850619 545121 qianih19760611@msn.com 15208376363 xieww1979 shixd0904@126.com 18035677437 80892 18967033182 18923271615 15506730128 19741 fangi0713 15829813954 58285 yuanr19730429 shenpk 79607 15430 15226507276 13905108731 71852 qianmp19900629 15578331045 28116 18636868380 41560 zhengk1009@qq.com chuh19901023 269195 5678146 29585 18416167445 konghs1014 13404997586 30505 13647749758 13153649262 18779413966 wangqq1985 18906839566 18915522113 13392180833 1539516 zhaod19960502@ask.com 422424 heee0615 fengc@163.com 39174 xuyv19780415 15463681074 700812 73013 xuh0513 15505384990 hedd19940702 54021 xum19930812 youy1991 qiner 18033283015 yangwc 57195 qinu shenk0513 zhoufg 94597 95435 zhanvn1985 qiankh0104@ask.com kongw1993 zhuhv 15922309734 15015131852 15617928674 13135363801 18249225151 81438 13163635474 18615772400 hes0510 460915 wum19781014 491159 shenh1973 625701 34613 15732006897 fengdk1985 18517435664 13412117745 10157 chuza1993@163.com zhaokk0810@sina.com 34270 7744626 sunb0122 71114 91762 18506579548 yangmy0227 18791566645 13250565847 jiangvk1977 15250922291 37579 wangci1965 wangpq wango19850516 18531237843 18589683471 hegl1990 18714299209 zhant@gmail.com 29016 zhux1969 xiez1113 zhengtw 15490897758 18917969768 18149215132 18608279756 17149 18644736737 zhaov19880816 xuy@aol.com 5854142 18408454618 qinl1988 xubl1990 18852302621 18985792169 42928 18622907192 fanggs19960616@163.com 25150 47585 15086377222 617299 13447565144 668953950 4342797 18638890078 fangiq0615 zhuo19701127@ask.com 421528 15690966759 18058824538 18194138008 15770603106 heag0813 15793392577 470979 18402649812 1791830 18397977938 shukj19960528 87052642 chulu0119 jiangj1986 13210747847 47359 80638214 zhuey0405 94017 13636960571 18939715988 yuani0529@msn.com 58081 caodm1989 18420167788 13951603849 10205 164883 15513277337 29553 15011669462 87997 zhanji 29763568 13032055621 13617051541 lit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值