zoukankan      html  css  js  c++  java
  • java生成随机汉字

    方法一:

    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下载
    jpinyin
    JPinyin是一个汉字转拼音的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
     }
    }
  • 相关阅读:
    Qtranslate是Win10系统的一款非常小众的翻译神器,真的是办公人员利器
    大文件查找软件(WizTree) v3.37 ---非常快
    DirPrintOK --- 将文件以树的形式列出、可导出到excel、html文件,用于整理电脑的文件,非常好用
    Linux五大类常用命令
    Android pm list 命令查看手机安装的apk信息
    Android adb命令列出当前设备所有apk安装的路径和包名
    Win10 Python2.7.6 如何使用pip命令?如何离线安装第三方模块?PyCharm设置Python2.7.6,、wxPython安装
    证件照在线免费移除背景或更换背景
    Ubuntu wps办公软件快捷键
    根据书籍的ISBN号查询书籍信息
  • 原文地址:https://www.cnblogs.com/chenglc/p/6923045.html
Copyright © 2011-2022 走看看