A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ye361571404 中级黑马   /  2014-9-21 17:41  /  1851 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

需要用的jar包pinyin4j-2.5.0.jar

  1. package com.itheima.log.demo;

  2. import java.util.Scanner;

  3. import net.sourceforge.pinyin4j.PinyinHelper;
  4. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  5. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  6. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  7. import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  8. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

  9. public class Demo01 {
  10.         public static void main(String[] args) {

  11.                 while(true){
  12.                         System.out.println("请输入中文字符。(退出输入exit)");
  13.                         //1.接收要翻译为拼音的字符串
  14.                         Scanner scanner = new Scanner(System.in);
  15.                         String zhongwen = scanner.nextLine();
  16.                         if(zhongwen.toLowerCase().equals("exit")){
  17.                                 break;
  18.                         }
  19.                         //接收中文字符转换后的拼音
  20.                         String pinyin = getPinYin(zhongwen);
  21.                         //打印输出
  22.                         System.out.println(pinyin);
  23.                 }
  24.         }

  25.         /**
  26.          *
  27.          * 方法描述:中文转换拼音
  28.          * 修改时间:2014-9-21下午05:02:02
  29.          * 修改备注:
  30.          * @param zhongwen        中文字符
  31.          * @return        转换后拼音字符
  32.          */
  33.         private static String getPinYin(String zhongwen) {
  34.                 //2.定义char[]数组接收字符串转换后的数组
  35.                 char[] chs = zhongwen.toCharArray();
  36.                 //3.定义String[]数组接收单个汉字转换后的拼音
  37.                 String[] pinyin = new String[1];
  38.                 //4.定义将转换后的拼音拼接到convert字符串变量中
  39.                 StringBuffer convert = new StringBuffer();
  40.                 try {
  41.                         //5.设置汉字拼音输出的格式
  42.                         HanyuPinyinOutputFormat hpof = new HanyuPinyinOutputFormat();
  43.                         hpof.setCaseType(HanyuPinyinCaseType.LOWERCASE);                        //设置大小写格式为:小写
  44.                         hpof.setToneType(HanyuPinyinToneType.WITHOUT_TONE);                        //设置声调格式为:无声调表示
  45.                         hpof.setVCharType(HanyuPinyinVCharType.WITH_V);                                //设置特殊拼音ü的显示格式为:以V表示该字符,例如:lv
  46.                        
  47.                         for(int i=0;i<chs.length;i++){
  48.                                 //6.判断是否为中文字符,如果是则转换为拼音,然后将拼音拼接到convert变量中
  49.                                 if(Character.toString(chs[i]).matches("[\\u4E00-\\u9FA5]+")){
  50.                                         pinyin = PinyinHelper.toHanyuPinyinStringArray(chs[i],hpof);
  51.                                         convert.append(pinyin[0]+" ");
  52.                                 //7.如果不是,直接将该字符拼接到convert变量中
  53.                                 }else {
  54.                                         convert.append(Character.toString(chs[i]));
  55.                                 }
  56.                         }
  57.                 } catch (BadHanyuPinyinOutputFormatCombination e) {
  58.                         e.printStackTrace();
  59.                 }
  60.                 return convert.toString();
  61.         }


  62. }
复制代码


pinyin4j.rar

171.68 KB, 下载次数: 346

13 个回复

倒序浏览
学习了~~
回复 使用道具 举报

互相学习嘛
回复 使用道具 举报
好深奥,学习下
回复 使用道具 举报
学习下……
回复 使用道具 举报
受教了。。。。。。。
回复 使用道具 举报
这个貌似在哪见过……
回复 使用道具 举报
原来还有汉语拼音包   import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
回复 使用道具 举报
真的很好
回复 使用道具 举报
哥哥,我真的受教了,太感谢了!!!
回复 使用道具 举报
这的不错哦!!!哥哥你黑马毕业了吗
回复 使用道具 举报
天的安排 发表于 2014-9-27 22:28
这的不错哦!!!哥哥你黑马毕业了吗

正在为进黑马做准备,你也加油
回复 使用道具 举报
天的安排 发表于 2014-9-27 22:23
哥哥,我真的受教了,太感谢了!!!

这个也只是把我能理解的东西加上注释,方便以后回过,和大家共同学习
回复 使用道具 举报
收藏学习了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马