用Java语言将一个键盘输入的数字转化成中文输出


Posted in 面试题 onJanuary 25, 2013
将一个键盘输入的数字转化成中文输出
(例如:输入:1234567 输出:一百二拾三万四千五百六拾七)
用java语言实现,,请编一段程序实现!
1. public class Reader {
2. private String strNum;
3.
4. private String strNumChFormat;
5.
6. private String strNumTemp;
7.
8. private int intNumLen;
9.
10. private String strBegin;
11.
12. public Reader(String strNum) {
13. this.strNum = strNum;
14. }
15.
16. public boolean check(String strNum) {
17. boolean valid = false;
18. if (strNum.substring(0, 1).equals(“0″)) {
19. this.strNum = strNum.substring(1);
20. }
21. try {
22. new Double(strNum);
23. valid = true;
24. } catch (NumberFormatException ex) {
25. System.out.println(“Bad number format!”);
26. }
27. return valid;
28. }
29.
30. public void init() {
31. strNumChFormat = “”;
32. intNumLen = strNum.length();
33. strNumTemp = strNum;
34. strNumTemp = strNumTemp.replace(’1′, ‘一’);
35. strNumTemp = strNumTemp.replace(’2′, ‘二’);
36. strNumTemp = strNumTemp.replace(’3′, ‘三’);
37. strNumTemp = strNumTemp.replace(’4′, ‘四’);
38. strNumTemp = strNumTemp.replace(’5′, ‘五’);
39. strNumTemp = strNumTemp.replace(’6′, ‘六’);
40. strNumTemp = strNumTemp.replace(’7′, ‘七’);
41. strNumTemp = strNumTemp.replace(’8′, ‘八’);
42. strNumTemp = strNumTemp.replace(’9′, ‘九’);
43. strNumTemp = strNumTemp.replace(’0′, ‘零’);
44. strNumTemp = strNumTemp.replace(‘.’, ‘点’);
45. strBegin = strNumTemp.substring(0, 1);
46. }
47.
48. public String readNum() {
49. if (check(strNum)) {
50. init();
51. try {
52. for (int i = 1, j = 1, k = 1; i 53. if (strNumTemp.charAt(intNumLen – 1) == ‘零’ && i == 1) {
54. strNumChFormat = “位”;
55. } else if (strNumTemp.charAt(intNumLen – i) == ‘零’ && j == 1) {
56. strNumChFormat = “位” + strNumChFormat;
57. } else if (strNumTemp.charAt(intNumLen – i) == ‘点’) {
58. j = 1;
59. k = 1;
60. strNumChFormat = strNumTemp.charAt(intNumLen – i) + strNumChFormat;
61. continue;
62. } else {
63. strNumChFormat = strNumTemp.charAt(intNumLen – i) + strNumChFormat;
64. }
65. if (strNumTemp.charAt(intNumLen – i – 1) != ‘位’ && strNumTemp.charAt(intNumLen – i – 1) != ‘零’) {
66. if (j == 1 && i 67. strNumChFormat = ‘拾’ + strNumChFormat;
68. } else if (j == 2 && i 69. strNumChFormat = ‘百’ + strNumChFormat;
70. } else if (j == 3 && i 71. strNumChFormat = ‘千’ + strNumChFormat;
72. }
73. }
74. if (j == 4 && i 75. j = 0;
76. }
77. if (k == 4 && i 78. strNumChFormat = ‘万’ + strNumChFormat;
79. } else if (k == 8 && i 80. k = 0;
81. strNumChFormat = ‘亿’ + strNumChFormat;
82. }
83. j++;
84. k++;
85. }
86. while (strNumChFormat.indexOf(“位”) != -1) {
87. strNumChFormat = strNumChFormat.replaceAll(“位”, ” “);
88. }
89. if (strNumChFormat.substring(0, 2) == “一拾”) {
90. strNumChFormat = strNumChFormat.substring(1, strNumChFormat.length());
91. }
92. if (strNumChFormat.indexOf(“点”) >= 0) {
93. String rebegin = strNumChFormat.substring(0, strNumChFormat.indexOf(“点”));
94. String relast = strNumChFormat.substring(strNumChFormat.indexOf(“点”), strNumChFormat.length());
95. for (int i = 1; i 96. relast = relast.replaceAll(“拾”, “”);
97. relast = relast.replaceAll(“百”, “”);
98. relast = relast.replaceAll(“千”, “”);
99. relast = relast.replaceAll(“万”, “”);
100. relast = relast.replaceAll(“亿”, “”);
101. }
102. strNumChFormat = rebegin + relast;
103. }
104. } catch (ArrayIndexOutOfBoundsException ex) {
105. ex.printStackTrace();
106. } catch (Exception ex) {
107. ex.printStackTrace();
108. }
109. int off = strNumChFormat.indexOf(“点”);
110. strNumChFormat = strBegin + strNumChFormat.substring(0);
111. } else {
112. strNumChFormat = “”;
113. }
114. return strNumChFormat;
115. }
116.
117. public static void main(String args[]) {
118. try {
119. String number = args[0].toString();
120. System.out.println(“The number is: ” + number);
121. Reader reader = new Reader(number);
122. System.out.println(“Output String: ” + reader.readNum());
123. } catch (Exception ex) {
124. System.out.println(“Please input like that: javac Reader ”);
125. }
126. }
127. }

Tags in this post...

面试题 相关文章推荐
数据库连接池的工作原理
Sep 26 面试题
Java面试中常遇到的问题,也是需要注意的几点
Aug 30 面试题
*p++ 自增p 还是p所指向的变量
Jul 16 面试题
写出二分查找算法的两种实现
May 13 面试题
西部世纪.net笔试题面试题
Apr 03 面试题
编程实现去掉XML的重复结点
May 28 面试题
Linux文件操作命令都有哪些
Jul 23 面试题
如果让你测试一台高速激光打印机,你都会进行哪些测试
Dec 04 面试题
如何获得EntityManager
Feb 09 面试题
如何用JQuery进行表单验证
May 29 面试题
用JAVA实现一种排序,JAVA类实现序列化的方法(二种)
Apr 23 面试题
95%的面试官都会问到的50道Java线程题,附答案
Aug 03 面试题
如何写出高质量、高性能的MySQL查询
Nov 17 #面试题
几个MySql的面试题
Apr 22 #面试题
简单叙述一下MYSQL的优化
May 09 #面试题
MySQL面试题目集锦
Apr 14 #面试题
如何提高MySql的安全性
Jun 19 #面试题
介绍一下Mysql的存储引擎
Feb 12 #面试题
介绍一下如何优化MySql
Dec 20 #面试题
You might like
php MySQL与分页效率
2008/06/04 PHP
php实现图片局部打马赛克的方法
2015/02/11 PHP
PHP使用token防止表单重复提交的方法
2016/04/07 PHP
浅谈php中urlencode与rawurlencode的区别
2016/09/05 PHP
用JavaScript事件串连执行多个处理过程的方法
2007/03/09 Javascript
List all the Databases on a SQL Server
2007/06/21 Javascript
jQuery实现公告文字左右滚动的实例代码
2013/10/29 Javascript
jquery实现html页面 div 假分页有原理有代码
2014/09/06 Javascript
用js实现放大镜的效果的简单实例
2016/05/23 Javascript
javascript获取网页各种高宽及位置的方法总结
2016/07/27 Javascript
vue 点击按钮实现动态挂载子组件的方法
2018/09/07 Javascript
vue组件实践之可搜索下拉框功能
2018/11/25 Javascript
ES6中Set和Map数据结构,Map与其它数据结构互相转换操作实例详解
2019/02/28 Javascript
微信小程序中使用Async-await方法异步请求变为同步请求方法
2019/03/28 Javascript
python开发的小球完全弹性碰撞游戏代码
2013/10/15 Python
理解python正则表达式
2016/01/15 Python
python下setuptools的安装详解及No module named setuptools的解决方法
2017/07/06 Python
python的文件操作方法汇总
2017/11/10 Python
flask + pymysql操作Mysql数据库的实例
2017/11/13 Python
python3中eval函数用法使用简介
2019/08/02 Python
用Python调用win命令行提高工作效率的实例
2019/08/14 Python
Python定时任务框架APScheduler原理及常用代码
2020/10/05 Python
pycharm激活码免费分享适用最新pycharm2020.2.3永久激活
2020/11/25 Python
python中altair可视化库实例用法
2021/01/26 Python
整个世界的设计师家具在哈恩:Designathome
2019/03/25 全球购物
经典促销广告词大全
2014/03/19 职场文书
活动策划求职信模板
2014/04/21 职场文书
建筑结构施工求职信
2014/07/11 职场文书
2014银行授权委托书样本
2014/10/04 职场文书
2014小学数学教研组工作总结
2014/12/06 职场文书
社会实践活动总结格式
2015/05/11 职场文书
2016幼儿园毕业感言
2015/12/08 职场文书
入党心得体会
2019/06/20 职场文书
nginx location中多个if里面proxy_pass的方法
2021/03/31 Servers
Java 实现限流器处理Rest接口请求详解流程
2021/11/02 Java/Android
Redis中有序集合的内部实现方式的详细介绍
2022/03/16 Redis