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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM