方法一:

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
}
}

java生成随机汉字的更多相关文章

  1. java生成随机序列号

    1.java生成随机序列号 String deleteUuid = UUID.randomUUID().toString(); 引用Jar包 //java-uuid-generator-3.1.3.j ...

  2. springboot搭建项目,实现Java生成随机图片验证码。

    这篇文章主要介绍了如何通过Java如何生成验证码并验证.验证码的作用我想必大家都知道,话不多说开始实施! 首先创建一个springboot项目以下是项目结构,内有utli工具类.存放生成图片验证码方法 ...

  3. Java生成随机验证码

    package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  4. java生成随机字符串

    学习java comparable特性时候,定义如下Student类,需要需要随机添加学生姓名以及学号和成绩,这是java如何随机生成名字,根据我的查询,我找到目前java库支持两种方法. 1. or ...

  5. java 生成随机校验码

    1 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.Buffe ...

  6. 如何用java生成随机验证码

     1.VerifyCode 类:   1 package com.HRuinger.enity;                          ImageIO.write(image, " ...

  7. java生成随机六位数的验证码&随机生成十位数ValidCode码,用于邮件的验证&检查是不是符合为合法的中国的手机号码

    package com.demo.test1; import java.security.NoSuchAlgorithmException; import java.security.SecureRa ...

  8. java生成随机字符

    1.生成的字符串每个位置都有可能是str中的一个字母或数字,需要导入的包是import java.util.Random; //length用户要求产生字符串的长度 public static Str ...

  9. Java生成随机图片验证码

    前台html代码 [Java] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 <div style="margin-top: 50px;&quo ...

随机推荐

  1. xtu summer individual 1 D - Round Numbers

    D - Round Numbers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u D ...

  2. python-001 第一个Python3.x程序 hello world

    我们可以使用以下命令来查看我们使用的Python版本: (d:\ProgramData\Anaconda3) C:\Users\Administrator.2016-20160920ET>pyt ...

  3. Leetcode 236.二叉树的最近公共祖先

    二叉树的最近公共祖先 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x ...

  4. Triangular Pastures (二维01背包)

    描述Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectang ...

  5. 【ITOO 2】.NET 动态建库建表:使用SQL字符串拼接方式

    导读:在最近接手的项目(高效云平台)中,有一个需求是要当企业用户注册时,给其动态的新建一个库和表.刚开始接手的时候,是一点头绪都没有,然后查了一些资料,也问了问上一版本的师哥师姐,终于有了点头绪.目前 ...

  6. C# 中的新增功能

    百度搜索:C# 中的新增功能 微软有站点专门介绍:C# 中的新增功能. 地址:https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/inde ...

  7. [UOJ#274][清华集训2016]温暖会指引我们前行

    [UOJ#274][清华集训2016]温暖会指引我们前行 试题描述 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了一 ...

  8. POJ1201:Intervals【差分约束】

    题目大意:给出N个闭区间,每个区间给出一个ci值,让你找出最小的数集Z使得每个闭区间都有不少于ci个Z中的元素,求card(Z) 思路:06年集训队论文<浅析差分约束系统>有详细的解题,设 ...

  9. Eclipse安装插件长时间停留在calculating requirements and dependencies

    如果安装插件的时候,Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 ) 这个问题通常就是在点击安装之后显示" ...

  10. Codevs 二叉树遍历问题 合集

    2010 求后序遍历 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 白银 Silver   题目描述 Description 输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列. ...