用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...

面试题 相关文章推荐
比较基础的php面试题及答案-填空题
Apr 26 面试题
普通PHP程序员笔试题
Jan 01 面试题
一个C/C++编程面试题
Nov 10 面试题
为什么UNION ALL比UNION快
Mar 17 面试题
商得四方公司面试题(gid+)
Apr 30 面试题
什么是Assembly(程序集)
Sep 14 面试题
C#面试题
May 06 面试题
介绍一下linux的文件权限
Feb 15 面试题
UML设计模式笔试题
Jun 07 面试题
几道Web/Ajax的面试题
Nov 05 面试题
请写出一段Python代码实现删除一个list里面的重复元素
Dec 29 面试题
若通过ObjectOutputStream向一个文件中多次以追加方式写入object,为什么用ObjectInputStream读取这些object时会产生StreamCorruptedException?
Oct 17 面试题
如何写出高质量、高性能的MySQL查询
Nov 17 #面试题
几个MySql的面试题
Apr 22 #面试题
简单叙述一下MYSQL的优化
May 09 #面试题
MySQL面试题目集锦
Apr 14 #面试题
如何提高MySql的安全性
Jun 19 #面试题
介绍一下Mysql的存储引擎
Feb 12 #面试题
介绍一下如何优化MySql
Dec 20 #面试题
You might like
WindowsXP中快速配置Apache+PHP5+Mysql
2008/06/05 PHP
解析PHP自带的进位制之间的转换函数
2013/06/08 PHP
PHP实现根据时间戳获取周几的方法
2016/02/26 PHP
php获取一定范围内取N个不重复的随机数
2016/05/28 PHP
php实现文章置顶功能的方法
2016/10/20 PHP
微信支付之JSAPI公众号支付详解
2019/05/15 PHP
javascript下给元素添加事件的方法与代码
2007/08/13 Javascript
使用ExtJS技术实现的拖动树结点
2010/08/05 Javascript
ExtJs的Date格式字符代码
2010/12/30 Javascript
基于JQuery实现异步刷新的代码(转载)
2011/03/29 Javascript
深入理解JavaScript系列(31):设计模式之代理模式详解
2015/03/03 Javascript
jQuery+css3实现文字跟随鼠标的上下抖动
2015/07/31 Javascript
jquery实现点击向下展开菜单项(伸缩导航)效果
2015/08/22 Javascript
jQuery基本过滤选择器用法示例
2016/09/09 Javascript
基于JavaScript实现的折半查找算法示例
2017/04/14 Javascript
JS数组交集、并集、差集的示例代码
2017/08/23 Javascript
javaScript之split与join的区别(详解)
2017/11/08 Javascript
详细分析单线程JS执行问题
2017/11/22 Javascript
jQuery EasyUI 折叠面板accordion的使用实例(分享)
2017/12/25 jQuery
Vue 封装防刷新考试倒计时组件的实现
2020/06/05 Javascript
[02:25]DOTA2英雄基础教程 熊战士
2014/01/03 DOTA
[02:13] 完美世界DOTA2联赛PWL DAY5集锦
2020/11/03 DOTA
[55:11]完美世界DOTA2联赛PWL S2 SZ vs LBZS 第一场 11.26
2020/11/30 DOTA
全面了解python中的类,对象,方法,属性
2016/09/11 Python
python通过cookie模拟已登录状态的初步研究
2016/11/09 Python
python中安装模块包版本冲突问题的解决
2017/05/02 Python
python实现雨滴下落到地面效果
2018/06/21 Python
如何更优雅地写python代码
2019/07/02 Python
Selenium启动Chrome时配置选项详解
2020/03/18 Python
印度领先的在线时尚商店:Koovs
2016/08/28 全球购物
中国一家综合的外贸B2C电子商务网站:DealeXtreme(DX)
2020/03/10 全球购物
美国在线家具网站:GDFStudio
2021/03/13 全球购物
《搭石》教学反思
2014/04/07 职场文书
缅怀先烈主题班会
2015/08/14 职场文书
Oracle笔记
2021/04/05 Oracle
pytorch DataLoader的num_workers参数与设置大小详解
2021/05/28 Python