用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两种查询函数array/row的区别
Jun 03 面试题
Web Service面试题:如何搭建Axis2的开发环境
Jun 20 面试题
char型变量中能不能存贮一个中文汉字
Jul 08 面试题
2019史上最全Database工程师题库
Dec 06 面试题
开放系统互连参考模型
Jun 29 面试题
linux面试题参考答案(5)
Sep 01 面试题
MIS软件工程师的面试题
Apr 22 面试题
你经历的项目中的SCM配置项主要有哪些?什么是配置项?
Nov 04 面试题
ajax是什么及其工作原理
Feb 08 面试题
Java面试题:Java类的Main方法如果是Private将会怎么样
Aug 18 面试题
Java模拟试题
Nov 10 面试题
描述JSP和Servlet的区别、共同点、各自应用的范围
Oct 02 面试题
如何写出高质量、高性能的MySQL查询
Nov 17 #面试题
几个MySql的面试题
Apr 22 #面试题
简单叙述一下MYSQL的优化
May 09 #面试题
MySQL面试题目集锦
Apr 14 #面试题
如何提高MySql的安全性
Jun 19 #面试题
介绍一下Mysql的存储引擎
Feb 12 #面试题
介绍一下如何优化MySql
Dec 20 #面试题
You might like
受疫情影响 动画《Re从零开始的异世界生活》第二季延期至7月
2020/03/10 日漫
php adodb连接带密码access数据库实例,测试成功
2008/05/14 PHP
Windows下安装PHP单元测试环境PHPUnit图文教程
2014/10/24 PHP
PHP中你应该知道的require()文件包含的正确用法
2015/06/12 PHP
详解PHP实现执行定时任务
2015/12/21 PHP
老司机传授Ubuntu下Apache+PHP+MySQL环境搭建攻略
2016/03/20 PHP
js jquery数组介绍
2012/07/15 Javascript
jQuery Trim去除字符串首尾空字符的实现方法说明
2014/02/11 Javascript
实例代码详解javascript实现窗口抖动及qq窗口抖动
2016/01/04 Javascript
Bootstrap网格系统详解
2016/04/26 Javascript
利用Jquery队列实现根据输入数量显示的动画
2016/09/01 Javascript
Boostrap实现的登录界面实例代码
2016/10/09 Javascript
node.js实现的装饰者模式示例
2017/09/06 Javascript
基于VUE移动音乐WEBAPP跨域请求失败的解决方法
2018/01/16 Javascript
详解关于Vue2.0路由开启keep-alive时需要注意的地方
2018/09/18 Javascript
JavaScript 作用域实例分析
2019/10/02 Javascript
Vue实现简易计算器
2020/02/25 Javascript
了不起的11个JavaScript代码重构最佳实践小结
2021/01/11 Javascript
[43:26]完美世界DOTA2联赛PWL S2 Forest vs Rebirth 第二场 11.20
2020/11/23 DOTA
[01:06:19]DOTA2-DPC中国联赛定级赛 LBZS vs SAG BO3第二场 1月8日
2021/03/11 DOTA
Python实现数据库编程方法详解
2015/06/09 Python
Python实现二维有序数组查找的方法
2016/04/27 Python
使用Python从零开始撸一个区块链
2018/03/14 Python
浅谈关于Python3中venv虚拟环境
2018/08/01 Python
对numpy中的where方法嵌套使用详解
2018/10/31 Python
在pycharm下设置自己的个性模版方法
2019/07/15 Python
Flask框架搭建虚拟环境的步骤分析
2019/12/21 Python
利用Python将图片中扭曲矩形的复原
2020/09/07 Python
python的setattr函数实例用法
2020/12/16 Python
业务总经理岗位职责
2014/02/03 职场文书
《藤野先生》教学反思
2014/02/19 职场文书
春游踏青活动方案
2014/08/14 职场文书
捐款感谢信
2015/01/20 职场文书
人事任命通知书
2015/04/21 职场文书
2015年大学迎新晚会总结
2015/07/16 职场文书
工作简报格式范文
2015/07/21 职场文书