用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数据运算类型都有哪些
Nov 05 面试题
简述synchronized和java.util.concurrent.locks.Lock的异同
Dec 08 面试题
面向对象编程是如何提高软件开发水平的
May 06 面试题
怎样建立和理解非常复杂的声明?例如定义一个包含N 个指向返回 指向字符的指针的函数的指针的数组?
Mar 19 面试题
SQL里面IN比较快还是EXISTS比较快
Jul 19 面试题
广州品高软件.net笔面试题目
Apr 18 面试题
局域网标准
Sep 10 面试题
网上常见的一份Linux面试题(多项选择部分)
Feb 07 面试题
为什么要用EJB
Apr 17 面试题
有abstract方法的类一定要用abstract修饰吗
Mar 14 面试题
shell的种类有哪些
Apr 15 面试题
J2EE中常用的名词进行解释
Nov 09 面试题
如何写出高质量、高性能的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 mkdir()定义和用法
2009/01/14 PHP
Jquery知识点三 jquery表单对象操作
2011/01/17 Javascript
jquery图片放大镜功能的实例代码
2013/03/26 Javascript
jquery遍历table的tr获取td的值实现方法
2016/05/19 Javascript
基于JavaScript实现的插入排序算法分析
2017/04/14 Javascript
JS如何设置元素样式的方法示例
2017/08/28 Javascript
深入理解Vue 单向数据流的原理
2017/11/09 Javascript
Vue2.0用户权限控制解决方案的示例
2018/02/10 Javascript
JS中‘hello’与new String(‘hello’)引出的问题详解
2018/08/14 Javascript
微信公众号H5支付接口调用方法
2019/01/10 Javascript
vue实现codemirror代码编辑器中的SQL代码格式化功能
2019/08/27 Javascript
微信小程序引入VANT组件的方法步骤
2019/09/19 Javascript
vue封装可复用组件confirm,并绑定在vue原型上的示例
2019/10/31 Javascript
python实现异步回调机制代码分享
2014/01/10 Python
Python编程实现粒子群算法(PSO)详解
2017/11/13 Python
python实现批量按比例缩放图片效果
2018/03/30 Python
基于python requests库中的代理实例讲解
2018/05/07 Python
python实现爬取图书封面
2018/07/05 Python
django框架自定义用户表操作示例
2018/08/07 Python
详解python如何在django中为用户模型添加自定义权限
2018/10/15 Python
ERLANG和PYTHON互通实现过程详解
2019/07/05 Python
Python3并发写文件与Python对比
2019/11/20 Python
Python爬虫自动化获取华图和粉笔网站的错题(推荐)
2021/01/08 Python
CSS3中的clip-path使用攻略
2015/08/03 HTML / CSS
美国正版电视节目和电影在线观看:Hulu
2018/05/24 全球购物
英国最大的在线亚洲杂货店:Red Rickshaw
2020/03/22 全球购物
How to spawning asynchronous work in J2EE
2016/08/29 面试题
应届毕业生求职信
2013/11/30 职场文书
优秀党员主要事迹
2014/01/19 职场文书
村党建工作汇报材料
2014/11/02 职场文书
2014年小学英语教师工作总
2014/12/03 职场文书
求职自荐信怎么写
2015/03/04 职场文书
2016年精神文明建设先进个人事迹材料
2016/02/29 职场文书
2019大学生实习报告
2019/06/21 职场文书
使用Python+OpenCV进行卡类型及16位卡号数字的OCR功能
2021/08/30 Python
MySQ InnoDB和MyISAM存储引擎介绍
2022/04/26 MySQL