博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java生成随机汉字
阅读量:2384 次
发布时间:2019-05-10

本文共 5295 字,大约阅读时间需要 17 分钟。

方法一:

public static char getRandomChar() {        return (char) (0x4e00 + (int) (Math.random() * (0x9fa5 - 0x4e00 + 1)));    }

方法二:不常见的汉字

public static void main(String[] args) {        RandomHan han = new RandomHan();        System.out.println(han.getRandomHan());    }}class RandomHan {    private Random ran=new Random();    private final static int delta=0x9fa5-0x4e00+1;    public char getRandomHan() {        return (char) (0x4e00 + ran.nextInt(delta));    }

方法三,太麻烦

Random random = new Random();///随机数String[] rBase = { "0", "1", "2", "3", "4", "5", "6", "7", "8","9", "a", "b", "c", "d", "e", "f" };// 生成第1位的区码int r1 = random.nextInt(3) + 11; //生成11到14之间的随机数String str_r1 = rBase[r1];// 生成第2位的区码int r2;if (r1 == 13) {r2 = random.nextInt(7); //生成0到7之间的随机数} else {r2 = random.nextInt(16); //生成0到16之间的随机数}String str_r2 = rBase[r2];// 生成第1位的位码int r3 = random.nextInt(6) + 10; //生成10到16之间的随机数String str_r3 = rBase[r3];// 生成第2位的位码int r4;if (r3 == 10) {r4 = random.nextInt(15) + 1; //生成1到16之间的随机数} else if (r3 == 15) {r4 = random.nextInt(15); //生成0到15之间的随机数} else {r4 = random.nextInt(16); //生成0到16之间的随机数}String str_r4 = rBase[r4];System.out.println(str_r1 + str_r2 + str_r3 + str_r4);// 将生成机内码转换为汉字byte[] bytes = new byte[2];//将生成的区码保存到字节数组的第1个元素中String str_r12 = str_r1 + str_r2;int tempLow = Integer.parseInt(str_r12, 16);bytes[0] = (byte) tempLow;//将生成的位码保存到字节数组的第2个元素中String str_r34 = str_r3 + str_r4;int tempHigh = Integer.parseInt(str_r34, 16);bytes[1] = (byte) tempHigh;String ctmp = new String(bytes,"gb2312"); //根据字节数组生成汉字System.out.println("生成汉字:" + ctmp); /**汉字转拼音jpinyin下载jpinyinJPinyin是一个汉字转拼音的Java开源类库,在PinYin4j的功能基础上做了一些改进。【JPinyin主要特性】1、准确、完善的字库;Unicode编码从4E00-9FA5范围及3007(〇)的20903个汉字中,JPinyin能转换除46个异体字(异体字不存在标准拼音)之外的所有汉字;2、拼音转换速度快;经测试,转换Unicode编码从4E00-9FA5范围的20902个汉字,JPinyin耗时约100毫秒。3、多拼音格式输出支持;JPinyin支持多种拼音输出格式:带音标、不带音标、数字表示音标以及拼音首字母输出格式;4、常见多音字识别;JPinyin支持常见多音字的识别,其中包括词组、成语、地名等;5、简繁体中文转换 */ 例子:import java.io.UnsupportedEncodingException;import java.util.Random;import opensource.jpinyin.ChineseHelper;import opensource.jpinyin.PinyinFormat;import opensource.jpinyin.PinyinHelper;public class TestChineseCode {  public static void main(String[] args) {  // TODO Auto-generated method stub  try {   for(int i=0;i<10;i++){    System.out.print("第:"+(i+1)+"  个字:");    CreatChineseCode();   }     } catch (UnsupportedEncodingException e) {   // TODO Auto-generated catch block   e.printStackTrace();  } }  public static void CreatChineseCode() throws UnsupportedEncodingException{  Random random = new Random();        String[] rBase = { "0", "1", "2", "3", "4", "5", "6", "7", "8",                           "9", "a", "b", "c", "d", "e", "f" };        // 生成第1位的区码        int r1 = random.nextInt(3) + 11; //生成11到14之间的随机数        String str_r1 = rBase[r1];        // 生成第2位的区码        int r2;        if (r1 == 13) {            r2 = random.nextInt(7); //生成0到7之间的随机数        } else {            r2 = random.nextInt(16); //生成0到16之间的随机数        }        String str_r2 = rBase[r2];        // 生成第1位的位码        int r3 = random.nextInt(6) + 10; //生成10到16之间的随机数        String str_r3 = rBase[r3];        // 生成第2位的位码        int r4;        if (r3 == 10) {            r4 = random.nextInt(15) + 1; //生成1到16之间的随机数        } else if (r3 == 15) {            r4 = random.nextInt(15); //生成0到15之间的随机数        } else {            r4 = random.nextInt(16); //生成0到16之间的随机数        }        String str_r4 = rBase[r4];        System.out.println("区码+位码="+str_r1 + str_r2 + str_r3 + str_r4);               // 将生成机内码转换为汉字        byte[] bytes = new byte[2];        //将生成的区码保存到字节数组的第1个元素中        String str_r12 = str_r1 + str_r2;        int tempLow = Integer.parseInt(str_r12, 16);        bytes[0] = (byte) tempLow;        //将生成的位码保存到字节数组的第2个元素中        String str_r34 = str_r3 + str_r4;        int tempHigh = Integer.parseInt(str_r34, 16);        bytes[1] = (byte) tempHigh;        String ctmp = new String(bytes,"gb2312"); //根据字节数组生成汉字        System.out.println("生成汉字:" + ctmp);               //String s="中国的首都是北京";        String s="";        s=ctmp;        char [] c=ctmp.toCharArray();        //带音标   zhōng,guó,de,shǒu,dū,shì,běi,jīng        System.out.println(PinyinHelper.convertToPinyinString(s,",",PinyinFormat.WITH_TONE_MARK));     //用数字代替音标   zhong1,guo2,de5,shou3,du1,shi4,bei3,jing1     System.out.println(PinyinHelper.convertToPinyinString(s,",",PinyinFormat.WITH_TONE_NUMBER));            //不带音标  zhong,guo,de,shou,du,shi,bei,jing             System.out.println(PinyinHelper.convertToPinyinString(s, ",", PinyinFormat.WITHOUT_TONE));                    System.out.println( PinyinHelper.getShortPinyin(s));//输出拼音首字母  zgdsdsbj                        //System.out.println("是否是多音字:"+PinyinHelper.hasMultiPinyin('好'));//判断多音字   true           //判断多音字   true     System.out.println("是否是多音字:"+(PinyinHelper.hasMultiPinyin(c[0])==false ? "不是":"是"));                 //System.out.println(ChineseHelper.convertToSimplifiedChinese("東"));//繁体字转简体字  东                     //System.out.println( ChineseHelper.convertToTraditionalChinese("东"));//简体字转繁体字  東        //System.out.println(ChineseHelper.isTraditionalChinese('哈'));//判断是否为繁体字   false                System.out.println("是否是繁体字:"+((ChineseHelper.isTraditionalChinese(c[0])==false) ? "不是":"是"));//判断是否为繁体字   false }}

 

posted @
2017-05-31 10:49 阅读(
...) 评论(
...)

转载地址:http://shcab.baihongyu.com/

你可能感兴趣的文章
flask一个基本的http响应流程
查看>>
linux常见的文件及目录操作12个命令
查看>>
挂载ceph的rbd块存储作为本地磁盘块
查看>>
ceph的块设备的两种使用方式及代码示例
查看>>
查看python中模块的所有方法
查看>>
ceph对象存储的配置与S3、swift接口的使用
查看>>
python通过librados库通过底层的rados操作ceph的对象存储和块存储
查看>>
在客户端使用python来调用boto S3 API来操作librados库
查看>>
ceph存储数据的详细流程(CRUSH)
查看>>
linux内核模块详解
查看>>
ceph集群的扩展(centos7环境)
查看>>
linux命令之top(查看cpu、内存等负载)
查看>>
linux_详解find命令
查看>>
openstack的swift组件详解
查看>>
两大主流开源分布式存储的对比:GlusterFS vs. Ceph
查看>>
面试笔试动态规划问题--python篇
查看>>
linux下的svn常用命令使用指南
查看>>
阿里云iot事业部一面面经
查看>>
《云计算架构技术与实践》
查看>>
《云计算架构技术与实践》序言(李德毅院士)
查看>>